18

I have the book "beyond the C++ standard library" and there are no examples of multithreading using boost. Would somebody be kind enough to show me a simple example where two threads are executed using boost- lets say asynchronously?

Shog9
  • 156,901
  • 35
  • 231
  • 235
user997112
  • 29,025
  • 43
  • 182
  • 361
  • 3
    In answer to your third question, I recommend [C++ Concurrency in Action](http://www.manning.com/williams/) by [Anthony Williams](http://stackoverflow.com/users/5597/anthony-williams), particularly since you've tagged this [C++11]. – johnsyweb Sep 15 '12 at 12:33
  • Take a look at tutorials: http://www.boost.org/doc/libs/1_51_0/doc/html/thread.html – Maxim Egorushkin Sep 15 '12 at 13:06

1 Answers1

38

This is my minimal Boost threading example.

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

using namespace std;

void ThreadFunction()
{
    int counter = 0;

    for(;;)
    {
        cout << "thread iteration " << ++counter << " Press Enter to stop" << endl;

        try
        {
            // Sleep and check for interrupt.
            // To check for interrupt without sleep,
            // use boost::this_thread::interruption_point()
            // which also throws boost::thread_interrupted
            boost::this_thread::sleep(boost::posix_time::milliseconds(500));
        }
        catch(boost::thread_interrupted&)
        {
            cout << "Thread is stopped" << endl;
            return;
        }
    }
}

int main()
{
    // Start thread
    boost::thread t(&ThreadFunction);

    // Wait for Enter 
    char ch;
    cin.get(ch);

    // Ask thread to stop
    t.interrupt();

    // Join - wait when thread actually exits
    t.join();
    cout << "main: thread ended" << endl;

    return 0;
}
Alex F
  • 42,307
  • 41
  • 144
  • 212
  • Thats brilliant- that first line is so simple. I understand your comment relating to the line, but why is "join()" named so? Is that synchronous program flow? Main() won't terminate until your thread finishes? – user997112 Sep 15 '12 at 13:32
  • 3
    join synchronously waits for thread exit. Without this line, the program may exit while worker thread is still running. This thread will be killed by OS, but this is not clean solution. – Alex F Sep 15 '12 at 13:43
  • 1
    So, this code shows, how to: start thread, stop thread by sending interrupt request and waiting for its execution. Thread code must be responsive for interrupt requests. See also: http://www.boost.org/doc/libs/1_41_0/doc/html/thread/thread_management.html, Predefined Interruption Points – Alex F Sep 15 '12 at 13:45
  • 1
    whilst you are so knowledgeable I dont suppose you could answer the boost vs native Unix thread question? Thanks I appreciate it. – user997112 Sep 15 '12 at 14:04
  • 1
    I don't know about this. In the worst case, even if boost thread implementation is slightly slower on start/stop operations, thread itself should have the same performance in both cases. Boost threads are implemented on the native threads on any supported platform. – Alex F Sep 15 '12 at 14:14