...
The compliant solution uses a mutex to make each call to prevent concurrent access to the shared seed value used in rand() function.
| Code Block | ||
|---|---|---|
| ||
#include <pthread.h>
pthread_mutex_t rand_lock = PTHREAD_MUTEX_INITIALIZER;
int get_secret() {
int secret;
pthread_mutex_lock(&rand_lock) ;
secret = (rand() % 100) + 100;
pthread_mutex_unlock(&rand_lock);
return secret;
}
|
...
[N1401-C1X Draft] Section 7.21.2.1 rand() function, Section 7.21.4.6 getenv() function, Section 7.22.5.8 strtok() function, Section 7.22.6.2 strerror() function, Section 7.25.3.1 asctime() function, Section 7.25.3.2 ctime() function
[POSIX.1 Thread Safety ]
...