We've covered the "Assembly Language", "C" and "C++" of the C++ threading world, and now we are going to try and move beyond that.
In the "C++" example we showed a object that automatically managed a thread's life time and used the thread to calculate the Fibonacci sequence in the background. Using templates we should be able to make a generic version of the Fibonacci calculator thread.
If boost::threads represent the C of multithreaded programming, then RAII and automatically managed threads represent the C++ of multithreaded programming.
In the last article we promised that using more RAII would allow us to get this code even smaller and better to manage. Here is the result of that:
class threaded_class { public: threaded_class() : m_stoprequested(false), m_thread(boost::bind(&threaded_class::do_work, this)) //Note 2 { } ~threaded_class() {
If pthreads represent the assembly language of multithreading programming, then boost::threads represent the C of multithreaded programming.
Boost threads introduce some handy code saving features for the creation of threads, which is nice, but not as important as the RAII techniques they put to use for mutex management. In this case an example is worth a thousand words. Here is the same code we wrote in for pthreads rewritten for boost::threads:
class threaded_class { public: threaded_class()
(All articles in this series.)
This is the beginning of a series of articles on multithreaded programming in C++. In this first article we will look at pthreads, which are generally considered to be the "assembly language of threading." We will start at the bottom and work our way up to higher concepts.
Recent comments
13 hours 23 min ago
18 hours 27 min ago
18 hours 56 min ago
1 day 11 hours ago
1 day 18 hours ago
1 week 6 days ago
3 weeks 12 hours ago
3 weeks 1 day ago
3 weeks 3 days ago
3 weeks 4 days ago