Learning threads on Linux

Linux is a new platform for me. I have been coding on Windows in C ++ for several years and have become comfortable with multithreading on this platform.

In addition to C ++ 11 at the time when I need to learn C ++ on the linux platform.

Linux apparently uses pthreads for the most part - well, boost :: threads and QT also have their own threads there. But with C ++ 11 comes std :: thread, a completely new (cross-platform and C ++ standard) way of making threads.

So, I think I will have to learn pthreads and std :: threads. Ultimately, std :: thread seems more important, but there is a lot of outdated code, so I should know both.

To synchronize threads on Windows, I would use WaitForMultipleObjects to wait for a series of tasks to complete before continuing.

Is there a similar synchronization mechanism for pthreads? std :: topics?

I looked at pthread_join , and it looks like it has the ability to wait only one thread at a time. Perhaps I missed another pthread call?

+3
c ++ linux pthreads c ++ 11 stdthread
May 30 '12 at 14:24
source share
5 answers

std::thread boost::thread adopted in C ++ 11 with some additional features. I understand that if boost::thread is replaced with std::thread code, it should still compile and work.

boost::thread based on the pthreads design, providing subtle C ++ wrappers over thread, mutexes and condition variables. Cancellation of the topic, although it was left outside of C ++ 11, because there was no agreement on how it should work in C ++.

So, by studying pthreads , you will also learn the concepts of std::thread . std::thread adds mostly syntactic sugar and convenience functions on top of the pthreads C API.

As for WaitForMultipleObjects() , neither pthreads nor std::thread provide anything similar to its bWaitAll=FALSE mode, however it is usually modeled using pipes and select() on UNIX or more recent eventfd() and epoll() on Linux . The bWaitAll=TRUE mode can be simulated, waiting for all tasks in turn, since it does not work until all objects are ready anyway.

+8
May 30 '12 at 14:36
source share

As for WaitForMultipleObjects , this is usually called barrier synchronization. Boost has an implementation called a barrier. It uses conditional variables to implement it, in posix its a pthread_cond_t

Here is the answer I recently explained with an explanation of barrier synchronization.

+1
May 30 '12 at 14:35
source share

No, neither pthreads nor C ++ 11 have the direct equivalent of WaitForMultipleObjects (that is, they are waiting for any type of descriptor). pthread_join can only be used to join threads and to only one specific thread.

The closest equivalent on posix platforms is to wait for multiple file descriptors using system calls such as select() , poll() or Linux-specific epoll() , but they require that you have a file descriptor to wait for, which is great for input events - output, but requires additional work for you to use them in anticipation of mutexes, variable conditions, or other synchronization objects. There are more common event libraries built on top of these system calls, for example. libevent and libev and Boost ASIO , supporting waiting for timers, as well as I / O, but still not ending the stream, locking mutexes, etc. using a single function like WaitForMultipleObjects

The alternatives that you have for pthreads and C ++ 11 are to wait for different types of synchronization separately. You can wait for timers, wait for threads to finish, wait for mutexes, wait for variable conditions, wait for asynchronous results to be ready ( std::async in C ++ 11, no direct equivalent in pthreads) ... but there is no call that allows you to wait at the same time heterogeneous set of these types.

+1
May 30 '12 at
source share

I could give you a really bizarre answer, but alas, it was there that I studied them, and this is a good introduction:

http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html

You use pthread_mutex_t for synchronization, and pthread_join probably handles the expectation of a problem with multiple tasks. It works exactly as you expected.

0
May 30 '12 at 14:34
source share

Based on this , you should call pthread_join for each individual thread that you created. Or use mutexes if you need to synchronize your threads.

0
May 30 '12 at 14:38
source share



All Articles