Why can PTHREAD_COND_INITIALIZER be used to initialize a condition variable only when it is declared?

Since PTHREAD_COND_INITIALIZER is actually a structure initializer, it can be used to initialize a condition variable only when it is declared.

From : multithreaded programming with POSIX threads

Question : Failed to understand the above quote.
This is just a macro, why can't I use it to initialize a condition variable at runtime?
What is its structure initializer for anything?

+4
source share
2 answers

Since this is a structure initializer, you cannot use it to initialize a structure in a statement other than a declaration.

On my system, it is defined like this:

#define PTHREAD_COND_INITIALIZER {_PTHREAD_COND_SIG_init, {0}} 

Deployed and used, we see:

 pthread_cond_t p = PTHREAD_COND_INITIALIZER; // << ok! p = PTHREAD_COND_INITIALIZER; // << compiler error =\ 

I.e

 p = PTHREAD_COND_INITIALIZER; 

expands to:

 p = {_PTHREAD_COND_SIG_init, {0}}; 
+8
source

For g++ use the -std=c++0x option and your problem should be resolved.

0
source

Source: https://habr.com/ru/post/1398583/


All Articles