I remember using C++ pthread_mutex
's in ancient past (well, during undergraduate years). That was my entire exposure to multithreading. Well, that and a little of Java's Thread
That was till last week. Like everything else, C++ multithreading has been give a boost (pun, eh) with boost
library. Here, in nutshell is how it works:
- Every boost::thread requires an object of type callable, with a operator() overloaded.
- The object passed to a thread must be callable. If not, it's best to pass a shared_ptr to the object.
- The boost::thread object itself is not copyable. But it can be placed in a move-aware container. I found it best to create a vector of shared_ptr's to thread objects and pass them to boost::Thread_group using an add_thread method.
- Thread synchronization is achieved by simply calling join on Thread or join_all on Thread_group
for more on threads.