Last active
July 6, 2016 04:44
-
-
Save a3f/b4ac6b8b4d8c93ae65720cbb291408b2 to your computer and use it in GitHub Desktop.
Condition variable spurious wakeup example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <signal.h> | |
| #include <pthread.h> | |
| #include <stdio.h> | |
| #include <sys/param.h> | |
| #ifdef BSD | |
| #define INTERRUPT_CVAR SIGINFO | |
| #else | |
| #define INTERRUPT_CVAR SIGUSR1 | |
| #endif | |
| volatile sig_atomic_t var = 0; | |
| void sigint(int signo) { | |
| if (signo == SIGINT) | |
| return; | |
| var = 1; | |
| } | |
| int main(void) { | |
| pthread_cond_t cvar = PTHREAD_COND_INITIALIZER; | |
| pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; | |
| signal(SIGINT, sigint); | |
| signal(INTERRUPT_CVAR, sigint); | |
| while (var == 0) { | |
| puts("Waiting"); | |
| pthread_cond_wait(&cvar, &lock); | |
| if (var == 0) { | |
| puts("Spurious Wakeup"); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment