I want to implement an algorithm in c++ using two concurrent infinite loop:

the first infinite loop produces the upper bound on each iteration and the other infinite loop produces lower bound on each iteration. Both loops would terminate only when the the upper bound and lower bound meets.

What's the best way in c++98 (with boost thread) to achieve this? Thank you:)

Pseudo code (not sure if it is optimal):

    main starts
    create 2 thread 
    thread 1 runs INF_LOOP1(&lowerbound), which keeps updating the lower bound 
    thread 2 runs INF_LOOP2=(&upperbound), which updating the upper bound
    every 10 seconds, check if lowerbound=upperbound, if yes, halt! if no, let the LOOP continue

If you want to be sure to catch the point at which the upper and lower bounds are equal, then you have to check that condition every time you modify one or the other, and you have to make sure you don't modify both at the same time.

This is an interesting case because it does not have any shared data (each thread modifies its own data) but it has a shared condition (for termination) which is essentially the same as having shared data. That's probably the lesson that this exercise is supposed to teach you.

Once you realize that your upper and lower bounds are shared (through the condition) between the two threads, then it becomes a simple synchronization problem that is easily solved with a mutex.

You can represent your threads with boost::thread and the mutex with boost::mutex (which is locked with boost::unique_lock<boost::mutex>). There are plenty of examples and tutorials on using those. You can also follow the standard library examples, because the standard C++11 threads are virtually identical to boost-thread.

But if you are having trouble writing that code, just show us what you have so far and we can help you along. But we can't just give you ready-made code.

Great! Thank you sooo much:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.