线程同步的方法有哪些?Linux下实现线程同步的三种方法( 二 )


静态态初始化,pthread_cond_t cond = PTHREAD_COND_INITIALIER;
动态初始化,int pthread_cond_init(pthread_cond_t *cond,pthread_condattr_t *cond_attr);
2、等待条件成立 。释放锁,同时阻塞等待条件变量为真才行 。timewait()设置等待时间,仍未signal,返回ETIMEOUT(加锁保证只有一个线程wait)
int pthread_cond_wait(pthread_cond_t *cond,pthread_mutex_t *mutex);
int pthread_cond_timewait(pthread_cond_t *cond,pthread_mutex *mutex,const timespec *abstime);
4、激活条件变量 。pthread_cond_signal,pthread_cond_broadcast(激活所有等待线程)
int pthread_cond_signal(pthread_cond_t *cond);
int pthread_cond_broadcast(pthread_cond_t *cond); //解除所有线程的阻塞
5、清除条件变量 。无线程等待,否则返回EBUSY
int pthread_cond_destroy(pthread_cond_t *cond);
01[cpp] view plain copy02#include 03#include 04#include "stdlib.h"05#include "unistd.h"06pthread_mutex_t mutex;07pthread_cond_t cond;08void hander(void *arg)09{10free(arg);11(void)pthread_mutex_unlock(&mutex);12}13void *thread1(void *arg)14{15pthread_cleanup_push(hander, &mutex);16while(1)17{18printf("thread1 is runningn");19pthread_mutex_lock(&mutex);20pthread_cond_wait(&cond, &mutex);21printf("thread1 applied the conditionn");22pthread_mutex_unlock(&mutex);23sleep(4);24}25pthread_cleanup_pop(0);26}27void *thread2(void *arg)28{29while(1)30{31printf("thread2 is runningn");32pthread_mutex_lock(&mutex);33pthread_cond_wait(&cond, &mutex);34printf("thread2 applied the conditionn");35pthread_mutex_unlock(&mutex);36sleep(1);37}38}39int main()40{41pthread_t thid1,thid2;42printf("condition variable study!n");43pthread_mutex_init(&mutex, NULL);44pthread_cond_init(&cond, NULL);45pthread_create(&thid1, NULL, thread1, NULL);46pthread_create(&thid2, NULL, thread2, NULL);47sleep(1);48do49{50pthread_cond_signal(&cond);51}while(1);52sleep(20);53pthread_exit(0);54return 0;55}复制代码[cpp] view plain copy#include #include #include "stdlib.h"#include "unistd.h"pthread_mutex_t mutex;pthread_cond_t cond;void hander(void *arg){free(arg);(void)pthread_mutex_unlock(&mutex);}void *thread1(void *arg){pthread_cleanup_push(hander, &mutex);while(1){printf("thread1 is runningn");pthread_mutex_lock(&mutex);pthread_cond_wait(&cond, &mutex);printf("thread1 applied the conditionn");pthread_mutex_unlock(&mutex);sleep(4);}pthread_cleanup_pop(0);}void *thread2(void *arg){while(1){printf("thread2 is runningn");pthread_mutex_lock(&mutex);pthread_cond_wait(&cond, &mutex);printf("thread2 applied the conditionn");pthread_mutex_unlock(&mutex);sleep(1);}}int main(){pthread_t thid1,thid2;printf("condition variable study!n");pthread_mutex_init(&mutex, NULL);pthread_cond_init(&cond, NULL);pthread_create(&thid1, NULL, thread1, NULL);pthread_create(&thid2, NULL, thread2, NULL);sleep(1);do{pthread_cond_signal(&cond);}while(1);sleep(20);pthread_exit(0);return 0;}01#include 02#include 03#include "stdio.h"04#include "stdlib.h"05static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;06static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;07struct node08{09int n_number;10struct node *n_next;11}*head = NULL;12static void cleanup_handler(void *arg)13{14printf("Cleanup handler of second thread./n");15free(arg);16(void)pthread_mutex_unlock(&mtx);17}18static void *thread_func(void *arg)19{20struct node *p = NULL;21pthread_cleanup_push(cleanup_handler, p);22while (1)23{24//这个mutex主要是用来保证pthread_cond_wait的并发性25pthread_mutex_lock(&mtx);26while (head == NULL)27{28//这个while要特别说明一下,单个pthread_cond_wait功能很完善,为何29//这里要有一个while (head == NULL)呢?因为pthread_cond_wait里的线30//程可能会被意外唤醒,如果这个时候head != NULL,则不是我们想要的情况 。31//这个时候,应该让线程继续进入pthread_cond_wait32// pthread_cond_wait会先解除之前的pthread_mutex_lock锁定的mtx,33//然后阻塞在等待对列里休眠,直到再次被唤醒(大多数情况下是等待的条件成立34//而被唤醒,唤醒后,该进程会先锁定先pthread_mutex_lock(&mtx);,再读取资源35//用这个流程是比较清楚的36pthread_cond_wait(&cond, &mtx);37p = head;38head = head->n_next;39printf("Got %d from front of queue/n", p->n_number);40free(p);41}42pthread_mutex_unlock(&mtx); //临界区数据操作完毕,释放互斥锁43}44pthread_cleanup_pop(0);45return 0;46}47int main(void)48{49pthread_t tid;50int i;51struct node *p;52//子线程会一直等待资源,类似生产者和消费者,但是这里的消费者可以是多个消费者,而53//不仅仅支持普通的单个消费者,这个模型虽然简单,但是很强大54pthread_create(&tid, NULL, thread_func, NULL);55sleep(1);56for (i = 0; i

推荐阅读