Created
May 11, 2016 16:02
-
-
Save shihyu/813d13cc5803d259f74d29870f32f0ab to your computer and use it in GitHub Desktop.
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 <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