信号量
子线程被阻塞,主线程可以激活,这就是线程的同步问题。
信号量就可以实现线程同步
#include <stdio.h>#include <string.h>#include <stdlib.h>#include <pthread.h>#include <semaphore.h>char buf[1024] = {0};sem_t sem;int flag = 0;void *func(void *arg){sem_wait(&sem);while(!flag){printf("count: %d\n", strlen(buf));memset(buf, 0, sizeof(buf));sem_wait(&sem);}pthread_exit(NULL);}int main(int argc, char **argv){int ret = -1;pthread_t th = -1;sem_init(&sem, 0, 0);ret = pthread_create(&th, NULL, func, NULL);if(ret != 0){printf("pthread_create failed");exit(-1);}printf("please input: \n");while (scanf("%s", buf)){if(!strncmp(buf, "end", 3)){printf("process end\n");flag = 1;sem_post(&sem);break;}sem_post(&sem);}printf("wait for recycle thread\n");ret = pthread_join(th, NULL);if(ret != 0){printf("pthread_create failed");exit(-1);}printf("finish recycle thread\n");sem_destroy(&sem);return 0;}
互斥锁
互斥锁又叫做互斥量mutex
互斥锁: 对共享数据进行锁定,保证同一时刻只能有一个线程去操作。
注意: 互斥锁是多个线程一起去抢,抢到锁的线程先执行,没有抢到锁的线程需要等待,等互斥锁使用完释放后,其它等待的线程再去抢这个锁。
相关函数:
pthread_mutex_init 功能:初始化一个互斥锁
pthread_mutex_destroy 功能:销毁一个互斥锁
pthread_mutex_lock 功能:加锁
pthread_mutex_unlock 功能:尝试加锁
互斥锁和信号量的关系:可以认为互斥锁是一种特殊的信号量
#include <stdio.h>#include <string.h>#include <stdlib.h>#include <pthread.h>char buf[1024] = {0};pthread_mutex_t mutex;int flag = 0;void *func(void *arg){sleep(1);while(!flag){pthread_mutex_lock(&mutex);printf("count: %d\n", strlen(buf));memset(buf, 0, sizeof(buf));pthread_mutex_unlock(&mutex);sleep(1);}pthread_exit(NULL);}int main(int argc, char **argv){int ret = -1;pthread_t th = -1;pthread_mutex_init (&mutex, NULL);ret = pthread_create(&th, NULL, func, NULL);if(ret != 0){printf("pthread_create failed");exit(-1);}printf("please input: \n");while (1){pthread_mutex_lock(&mutex);scanf("%s", buf);pthread_mutex_unlock(&mutex);if(!strncmp(buf, "end", 3)){printf("process end\n");flag = 1;break;}sleep(1);}printf("wait for recycle thread\n");ret = pthread_join(th, NULL);if(ret != 0){printf("pthread_join failed");exit(-1);}printf("finish recycle thread\n");pthread_mutex_destroy(&mutex);return 0;}
条件变量
相关函数:
pthread_cond_init
pthread_cond_destory
pthread_cond_wait
pthread_cond_signal/pthread_cond_broadcast
#include <stdio.h>#include <string.h>#include <stdlib.h>#include <pthread.h>char buf[1024] = {0};pthread_mutex_t mutex;pthread_cond_t cond;int flag = 0;void *func(void *arg){while(!flag){pthread_mutex_lock(&mutex);pthread_cond_wait(&cond, &mutex);printf("count: %d\n", strlen(buf));memset(buf, 0, sizeof(buf));pthread_mutex_unlock(&mutex);//sleep(1);}pthread_exit(NULL);}int main(int argc, char **argv){int ret = -1;pthread_t th = -1;pthread_mutex_init(&mutex, NULL);pthread_cond_init(&cond, NULL);ret = pthread_create(&th, NULL, func, NULL);if(ret != 0){printf("pthread_create failed");exit(-1);}printf("please input: \n");while (1){//pthread_mutex_lock(&mutex);scanf("%s", buf);pthread_cond_signal(&cond);pthread_mutex_unlock(&mutex);if(!strncmp(buf, "end", 3)){printf("process end\n");flag = 1;break;}//sleep(1);}printf("wait for recycle thread\n");ret = pthread_join(th, NULL);if(ret != 0){printf("pthread_create failed");exit(-1);}printf("finish recycle thread\n");pthread_mutex_destroy(&mutex);pthread_cond_destroy(&cond);return 0;}
