What is pthread H on Linux

Thread with C ++ on Linux

This would be a correct example.

Okay, I have to pick it up now, because even in the corrected state this is a "lousy" style.

C includes have no place in C ++! Therefore ...

You should always use the C ++ variant of a C include, if available. Otherwise an "extern C" should be used around it.

Variables always miss an initial value!

The difference between C ++ and C in the case of the GCC (with its C99 standard) is that the GCC in the case of C initializes the variables with a default value (thanks to the kernel developers, who put a lot of pressure on the GCC developers ). Unfortunately, this does not apply to C ++ (g ++ or gcc -lstdc ++). In addition, the RAII principle (Resource Allocation is Initialization) applies in C ++ anyway ...

After creating a thread, the further procedure must be determined.

It all depends on what attributes the thread was created with. Either joinable or not. Then you MUST detach or join the thread (pthread_detach = pthread_raise_zombie: D), otherwise the behavior is undefined. With a detach you release the thread into the wild, so you can no longer get to it. So one has to make sure that it ends normally before the comprehensive process ends. A join waits for the thread to return normally. Is basically something like a "wait_for_thread" ...

Very important: If you have a GCC 4.4 or larger available, there is a high probability that the StdC ++ Lib Version 4 is included and it contains native C ++ threads. You should definitely use them, because pthreads are for C and not for C ++. The signal concept in pthreads struggles with C ++ exceptions.

Here is a C ++ 0x example.

You don't necessarily have to use a LockGuard, but it is best for this example because "cout" is used several times once. You can take out the LockGuard for a gag ... looks funny

It is compiled with "g ++ -std = c ++ 0x -lpthread ..." pthrads is a fallacy here, some remainder is probably used, because the native C ++ threads are not based on pthreads but on kernel threads.