# Linux中的标准库pthread的应用举例之多线程同步 **Repository Path**: allankun/pthread ## Basic Information - **Project Name**: Linux中的标准库pthread的应用举例之多线程同步 - **Description**: 启动两个线程,一个线程负责在终端依次显示hello的字符串里的字符,一个线程负责在终端依次显示hello的字符串里的字符 要求终端最后的显示顺序: hello world - **Primary Language**: C - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 1 - **Created**: 2022-03-26 - **Last Updated**: 2022-03-26 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Linux中的标准库pthread的应用举例之多线程同步 ## 介绍 启动两个线程,一个线程负责在终端依次显示hello字符串里的字符,一个线程负责在终端依次显示world字符串里的字符 要求终端最后的显示顺序: hello world ## pthread_join紧跟在pthread_create之后的情况 pthread_join会等待pthread_create的执行完成后在释放这个线程,否则一直等待。具有同步效果 ```C #include #include #include void printer(char *str) { while(*str!='\0') { putchar(*str); fflush(stdout); str++; sleep(1); } printf("\n"); } void *thread_fun_1(void *arg) { char *str = "hello"; printer(str); } void *thread_fun_2(void *arg) { char *str = "world"; printer(str); } int main(void) { pthread_t tid1, tid2; // 创建线程1,并时执行 pthread_create(&tid1, NULL, thread_fun_1, NULL); // 等待线程1执行完成,阻碍其他操作 pthread_join(tid1, NULL); // 创建线程2,并时执行 pthread_create(&tid2, NULL, thread_fun_2, NULL); // 等待线程2执行完成,阻碍其他操作 pthread_join(tid2, NULL); return 0; } ``` 执行结果 ![输入图片说明](pthread5.png) ## pthread_join没有紧跟在pthread_create之后的情况 连续启动两个pthread后在执行pthread_join,没有同步效果 ```c #include #include #include void printer(char *str) { while(*str!='\0') { putchar(*str); fflush(stdout); str++; sleep(1); } printf("\n"); } void *thread_fun_1(void *arg) { char *str = "hello"; printer(str); } void *thread_fun_2(void *arg) { char *str = "world"; printer(str); } int main(void) { pthread_t tid1, tid2; // 创建线程1,并时执行 pthread_create(&tid1, NULL, thread_fun_1, NULL); // 创建线程2,并时执行 pthread_create(&tid2, NULL, thread_fun_2, NULL); // 等待线程1执行完成,阻碍其他操作 pthread_join(tid1, NULL); // 等待线程2执行完成,阻碍其他操作 pthread_join(tid2, NULL); return 0; } ``` 执行结果 ![输入图片说明](pthread1.png) ## pthread_mutex_t方式控制两个线程同步 ```c #include #include #include pthread_mutex_t mutex; void printer(char *thread_name, char *str) { pthread_mutex_lock(&mutex); printf("%s : ",thread_name); while(*str!='\0') { putchar(*str); fflush(stdout); str++; sleep(1); } printf("\n"); pthread_mutex_unlock(&mutex); } void *thread_fun_1(void *arg) { char *str = "hello"; printer("thread_1",str); printf("thread_fun_1 end\n"); } void *thread_fun_2(void *arg) { char *str = "world"; printer("thread_2",str); printf("thread_fun_2 end\n"); } int main(void) { pthread_t tid1, tid2; pthread_mutex_init(&mutex, NULL); pthread_create(&tid1, NULL, thread_fun_1, NULL); pthread_create(&tid2, NULL, thread_fun_2, NULL); pthread_join(tid1, NULL); pthread_join(tid2, NULL); pthread_mutex_destroy(&mutex); return 0; } ``` 执行结果 ![输入图片说明](pthread2.png) ## semaphore的同步方式控制两个线程同步 ```c #include #include #include #include sem_t sem; void printer(char *thread_name, char *str) { printf("%s : ",thread_name); while(*str!='\0') { putchar(*str); fflush(stdout); str++; sleep(1); } printf("\n"); } void *thread_fun_1(void *arg) { sem_wait(&sem); // sem 取得 char *str = "hello"; printer("thread_1",str); printf("thread_fun_1 end\n"); } void *thread_fun_2(void *arg) { char *str = "world"; printer("thread_2",str); printf("thread_fun_2 end\n"); sem_post(&sem); // sem 释放 } /*--------------------------- semapthore 实现同步的实例 tid2线程在semapthore被释放之后, tid1线程可以继续执行 -----------------------------*/ int main(void) { pthread_t tid1, tid2; //semapthore的默认初始值是0. sem_init(&sem,0,0); pthread_create(&tid1, NULL, thread_fun_1, NULL); pthread_create(&tid2, NULL, thread_fun_2, NULL); pthread_join(tid1, NULL); pthread_join(tid2, NULL); sem_destroy(&sem); return 0; } ``` 执行结果 ![输入图片说明](pthread3.png) ## semaphore的互斥方式控制两个线程同步 ```c #include #include #include #include sem_t sem; void printer(char *thread_name, char *str) { sem_wait(&sem); // sem 取得 printf("%s : ",thread_name); while(*str!='\0') { putchar(*str); fflush(stdout); str++; sleep(1); } printf("\n"); sem_post(&sem); // sem 释放 } void *thread_fun_1(void *arg) { char *str = "hello"; printer("thread_1",str); printf("thread_fun_1 end\n"); } void *thread_fun_2(void *arg) { char *str = "world"; printer("thread_2",str); printf("thread_fun_2 end\n"); } /*--------------------------- semapthore 互斥方式验证实例 -----------------------------*/ int main(void) { pthread_t tid1, tid2; //semapthore的初始值设定成1 sem_init(&sem,0,1); pthread_create(&tid1, NULL, thread_fun_1, NULL); pthread_create(&tid2, NULL, thread_fun_2, NULL); pthread_join(tid1, NULL); pthread_join(tid2, NULL); sem_destroy(&sem); return 0; } ``` 执行结果 ![输入图片说明](pthread4.png) ## 实例程序的编译方式 linux终端如下执行 ```shell gcc pthread_1.c -o test1 -lpthread gcc pthread_2.c -o test2 -lpthread gcc pthread_3.c -o test3 -lpthread gcc pthread_4.c -o test4 -lpthread gcc pthread_5.c -o test5 -lpthread ```