Where to get simple Boost Thread Management example?

前端 未结 2 897
名媛妹妹
名媛妹妹 2021-01-16 12:26

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()
   {
      s         


        
相关标签:
2条回答
  • 2021-01-16 12:57

    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

    0 讨论(0)
  • 2021-01-16 13:04

    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();
    }
    
    0 讨论(0)
提交回复
热议问题