The current correct way to program threads in Linux

I know that the implementation of threads in the Linux kernel and libc has gone through major changes in the past. What is the best way to use streams today from C programs? (I don’t even know if there are several APIs that I can use - I just know pthreads)

I don’t care about the old versions of the kernel and libc, but I care about using multiple cores efficiently and about portability (I want my code to work with other Unix as well).

If I just use Posix threads as described in man 7 pthreads and restrict my POSIX API code, would that be ok?

edit : thanks to everyone who answers. I really thought of using some available thread pool library, but for this project this is really not an option.

+4
source share
3 answers

For the most part, yes. This is the POSIX point. Each platform you plan to port to (including up to the OS, kernel number, and architecture) may have some differences that you need to know about. This is your homework :)

In addition, structures such as Qt and library packages such as Boost make this work more elegant as tips. If you can integrate them, I highly recommend it.

+3
source

POSIX themes are a good choice and popular. You can also see Boost Threads and Intel Thread Building Blocks for other options when programming in C / C ++, and not for encoding directly in the POSIX API. In particular, Intel TBB claims to use multi-core processors in a simple and efficient way. I don’t have much experience with TBB, but I saw demos where using TBB significantly reduced the size and complexity of the code and was an order of magnitude faster than an implementation written in direct C ++ using POSIX streams written by an experienced engineer.

+2
source

Using threads is difficult and error prone. Avoid programming them if possible. A well-written article on this subject can be found here .

Multi-core parallelism can be achieved in a variety of ways besides the direct use of threads. The simplest is the multiprocessing parallelism process. For n cores, run n processes and split the workload between them. This is most appropriate when the tasks are coarse-grained and require little or no connection between them. When it should be in the process, you can use message passing for all messages and synchronization between threads and always try to pass immutable objects as messages.

0
source

All Articles