Skip to content

Instantly share code, notes, and snippets.

@nevali
Created March 12, 2013 22:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nevali/5147885 to your computer and use it in GitHub Desktop.
Save nevali/5147885 to your computer and use it in GitHub Desktop.
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