Skip to content

Instantly share code, notes, and snippets.

@shihyu
Created May 11, 2016 16:02
Show Gist options
  • Select an option

  • Save shihyu/813d13cc5803d259f74d29870f32f0ab to your computer and use it in GitHub Desktop.

Select an option

Save shihyu/813d13cc5803d259f74d29870f32f0ab to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
static pthread_mutex_t Cmtx = PTHREAD_MUTEX_INITIALIZER;
void* ap1_thread(void* args)
{
while (1) {
pthread_mutex_lock(&Cmtx);
printf("ap1_thread[+]\n");
pthread_mutex_unlock(&Cmtx);
sleep(1);
}
return NULL;
}
void* ap2_thread(void* args)
{
while (1) {
pthread_mutex_lock(&Cmtx);
printf("ap2_thread[-]\n");
pthread_mutex_unlock(&Cmtx);
sleep(1);
}
return NULL;
}
int main(int argc, char* argv[])
{
pthread_t thread1, thread2;
pthread_create(&thread1, NULL, ap1_thread, NULL);
pthread_create(&thread2, NULL, ap2_thread, NULL);
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment