1

So I have a simple cpp file. Only one with one main function and 3 int a-la public variables. like:

   int a;
   int b;
   int c;
   void main()
   {
      startThredA();
      startThredB();
      while(1)
     {
        c = a + b;
        printf(c);
     }
   }

I whant to create 2 threds A and B one of which should generate random value for A and another random value for b. How to do such thing?

Rella
  • 65,003
  • 109
  • 363
  • 636
  • what part of the [Boost.Thread](http://www.boost.org/doc/libs/1_44_0/doc/html/thread/thread_management.html) documentation do you not understand? – Sam Miller Nov 05 '10 at 01:22
  • @Sam - I don't understand the part where it's just a list of functions and classes without any examples. It's hardly a model of clarity, making the bar for entry higher than it ought to be. – Steve Townsend Nov 05 '10 at 01:37
  • 2
    @Steve: so what do you call the code samples at the very top of the page @Sam linked to? Such as the one under the heading "Launching Threads". I'd call it "an example of how to create a thread". Boost has a lot of libs with bad or confusing documentation, but Boost.Thread specifically is pretty clear, concise and easy to understand, imo. It does require you to read a bit of the docs, sure, but I think that's fair. – jalf Nov 05 '10 at 01:59

2 Answers2

3

Here is a small and simple example. It's tried and seems to work fine.

#include <iostream>
#include <boost/thread.hpp>

namespace this_thread = boost::this_thread;

int a = 0;
int b = 0;
int c = 0;

class BaseThread
{
public:
    BaseThread()
        { }
    virtual ~BaseThread()
        { }

    void operator()()
    {
        try
        {
            for (;;)
            {
                // Check if the thread should be interrupted
                this_thread::interruption_point();

                DoStuff();
            }
        }
        catch (boost::thread_interrupted)
        {
            // Thread end
        }
    }

protected:
    virtual void DoStuff() = 0;
};

class ThreadA : public BaseThread
{
protected:
    virtual void DoStuff()
    {
        a += 1000;
        // Sleep a little while (0.5 second)
        this_thread::sleep(boost::posix_time::milliseconds(500));
    }
};

class ThreadB : public BaseThread
{
protected:
    virtual void DoStuff()
    {
        b++;
        // Sleep a little while (0.5 second)
        this_thread::sleep(boost::posix_time::milliseconds(100));
    }
};

int main()
{
    ThreadA thread_a_instance;
    ThreadB thread_b_instance;

    boost::thread threadA = boost::thread(thread_a_instance);
    boost::thread threadB = boost::thread(thread_b_instance);

    // Do this for 10 seconds (0.25 seconds * 40 = 10 seconds)
    for (int i = 0; i < 40; i++)
    {
        c = a + b;
        std::cout << c << std::endl;

        // Sleep a little while (0.25 second)
        this_thread::sleep(boost::posix_time::milliseconds(250));
    }

    threadB.interrupt();
    threadB.join();

    threadA.interrupt();
    threadA.join();
}
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
2

There is a series of articles starting here that should give you some initial pointers. The writer is responsible in large part for shepherding boost.thread into C++0x.

List of the articles:

Multithreading in C++0x Part 1: Starting Threads

Multithreading in C++0x Part 2: Starting Threads with Function Objects and Arguments

Multithreading in C++0x Part 3: Starting Threads with Member Functions and Reference Arguments

Multithreading in C++0x Part 4: Protecting Shared Data

Multithreading in C++0x Part 5: Flexible locking with std::unique_lock<>

Multithreading in C++0x part 6: Lazy initialization and double-checked locking with atomics

Multithreading in C++0x part 7: Locking multiple mutexes without deadlock

Multithreading in C++0x part 8: Futures, Promises and Asynchronous Function Calls

Steve Townsend
  • 53,498
  • 9
  • 91
  • 140