Created
March 12, 2013 22:59
-
-
Save nevali/5147885 to your computer and use it in GitHub Desktop.
This file contains 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
struct baz | |
{ | |
pthread_mutex_t mutex; | |
pthread_t thread; | |
int statekeeping; | |
}; | |
int | |
foo(void) | |
{ | |
struct baz *bar; | |
if(!(bar = (struct baz *) calloc(1, sizeof(baz))) return -1; | |
pthread_mutex_init(&(bar->mutex), NULL); | |
pthread_mutex_lock(&(bar->mutex)); | |
pthread_create(&(bar->thread), NULL, mythread, bar); | |
bar->statekeeping = 1; | |
add_baz_to_global_list(bar); | |
pthread_mutex_transfer(&(bar->mutex), bar->thread); | |
return 0; | |
} | |
void * | |
mythread(void *arg) | |
{ | |
struct baz *bar; | |
bar = (struct baz *) arg; | |
pthread_mutex_lock(&(bar->mutex)); | |
bar->statekeeping = 2; /* up and running! */ | |
pthread_mutex_unlock(&(bar->mutex)); | |
/* do stuff */ | |
return NULL; | |
} | |
void * | |
some_other_thread(void *arg) | |
{ | |
struct baz *thingy; | |
thingy = get_a_baz(); | |
pthread_mutex_lock(&(thingy->mutex)); | |
if(thingy->statekeeping == 1) | |
{ | |
/* crazy limbo! wtf do we do now? */ | |
} | |
else if(thingy->statekeeping == 2) | |
{ | |
/* up and running! */ | |
} | |
else | |
{ | |
/* odd error condition */ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment