pthread_cond_init | PTHREAD_COND_INITIALIZER
创建并初始化条件变量
头文件:
#include <pthread.h>
原型:
int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *cond_attr);
参数:
pthread_cond_t *cond:存储初始化后的条件变量;
pthread_condattr_t *cond_attr:填NULL;
返回值:
成功,返回0;
失败,返回非0,设置errno;
例程:
//用宏的方式初始化
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
//用函数的方式初始化
static pthread_cond_t cond;
if(pthread_cond_init(&cond, NULL) != 0)
{
perror("pthread_cond_init");
return -1;
}
pthread_cond_wait
阻塞,等待唤醒
1.条件不满足,解开互斥锁
2.阻塞当前线程,等待被唤醒
pthread_cond_wait(&cond, &mutex);
3.被唤醒后,尝试上锁
4.如果上锁失败,则继续休眠
5.如果上锁成功,则从当前位置继续向下执行,不会从线程头部开始运行。
头文件:
#include <pthread.h>
原型:
int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);
参数:
pthread_cond_t *cond:指定的条件变量;
pthread_mutex_t *mutex:指定互斥锁;
返回值:
成功,返回0;
失败,返回非0,设置errno;
例程:
int flag = 1; //全局变量,1.打印字符A;2.打印字符B
while(1)
{ //上锁
pthread_mutex_lock(&mutex);
if(flag != 2)
{
//1.条件不满足,解开互斥锁
//2.阻塞当前线程,等待被唤醒
pthread_cond_wait(&cond, &mutex);
//3.被唤醒后,尝试上锁
//4.如果上锁失败,则继续休眠
//5.如果上锁成功,则从当前位置继续向下执行,不会从线程头部开始运行。
}
printf("B\n");
flag = 1;
pthread_mutex_unlock(&mutex);
pthread_cond_signal(&cond);
}
pthread_cond_signal
唤醒条件变量
头文件:
#include <pthread.h>
原型:
int pthread_cond_signal(pthread_cond_t *cond);
参数:
pthread_cond_t *cond:指定要唤醒的条件变量;
返回值:
成功,返回0;
失败,返回非0,设置errno;
例程:
int flag = 1; //全局变量,1.打印字符A;2.打印字符B
while(1)
{
pthread_mutex_lock(&mutex);
if(flag != 2)
{
pthread_cond_wait(&cond, &mutex);
}
printf("B\n");
flag = 1;
pthread_mutex_unlock(&mutex);
pthread_cond_signal(&cond);
}
pthread_cond_destroy
销毁条件变量
头文件:
#include <pthread.h>
原型:
int pthread_cond_destroy(pthread_cond_t *cond);
参数:
pthread_cond_t *cond:指定要销毁的互斥锁;
返回值:
成功,返回0;
失败,返回非0,设置errno;
例程:
pthread_cond_destroy(&cond);
应用例程1
一个线程打印,一个线程倒置,要求倒置一次打印一次
#include <stdio.h>
#include <pthread.h>
#include <string.h>
//临界资源
char str[12] = "1234---abcd";
//互斥量
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
//条件变量
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
int flag = 1;
//线程1打印
void *handler1(void *arg)
{
while(1){
pthread_mutex_lock(&mutex);
if(flag != 1)
pthread_cond_wait(&cond,&mutex);
printf("%s\n",str);
sleep(1);
flag = 2;
pthread_mutex_unlock(&mutex);
pthread_cond_signal(&cond);
}
pthread_exit(NULL);
}
//线程2倒置
void *handler2(void *arg)
{
char temp;
char *head = NULL,*tail = NULL;
while(1){
pthread_mutex_lock(&mutex);
if(flag != 2)
pthread_cond_wait(&cond,&mutex);
head = str,tail = str+strlen(str)-1;
while(head < tail)
{
temp = *head;
*head = *tail;
*tail = temp;
head++;
tail--;
}
flag = 1;
pthread_mutex_unlock(&mutex);
pthread_cond_signal(&cond);
}
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
pthread_t tid1,tid2;
if(pthread_create(&tid1,NULL,handler1,NULL)!=0)
{
perror("pthread_create_tid1");
return -1;
}
if(pthread_create(&tid2,NULL,handler2,NULL)!=0)
{
perror("pthread_create_tid2");
return -1;
}
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
//销毁互斥量
pthread_mutex_destroy(&mutex);
//销毁条件变量
pthread_cond_destroy(&cond);
return 0;
}
应用例程2
顺序循环打印 ABC
#include <stdio.h>
#include <pthread.h>
#include <string.h>
//互斥量
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
//条件变量
pthread_cond_t cond_A_B = PTHREAD_COND_INITIALIZER;
pthread_cond_t cond_B_C = PTHREAD_COND_INITIALIZER;
pthread_cond_t cond_C_A = PTHREAD_COND_INITIALIZER;
int flag = 1;
//线程1打印A
void *handler1(void *arg)
{
while(1){
pthread_mutex_lock(&mutex);
if(flag != 1)
pthread_cond_wait(&cond_C_A,&mutex);
printf("%c\n",'A');
sleep(1);
flag = 2;
pthread_mutex_unlock(&mutex);
pthread_cond_signal(&cond_A_B);
}
pthread_exit(NULL);
}
//线程2打印B
void *handler2(void *arg)
{
while(1){
pthread_mutex_lock(&mutex);
if(flag != 2)
pthread_cond_wait(&cond_A_B,&mutex);
printf("%c\n",'B');
sleep(1);
flag = 3;
pthread_mutex_unlock(&mutex);
pthread_cond_signal(&cond_B_C);
}
pthread_exit(NULL);
}
//线程3打印C
void *handler3(void *arg)
{
while(1){
pthread_mutex_lock(&mutex);
if(flag != 3)
pthread_cond_wait(&cond_B_C,&mutex);
printf("%c\n",'C');
sleep(1);
flag = 1;
pthread_mutex_unlock(&mutex);
pthread_cond_signal(&cond_C_A);
}
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
pthread_t tid1,tid2,tid3;
if(pthread_create(&tid1,NULL,handler1,NULL)!=0)
{
perror("pthread_create_tid1");
return -1;
}
if(pthread_create(&tid2,NULL,handler2,NULL)!=0)
{
perror("pthread_create_tid2");
return -1;
}
if(pthread_create(&tid3,NULL,handler3,NULL)!=0)
{
perror("pthread_create_tid3");
return -1;
}
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
pthread_join(tid3,NULL);
//销毁互斥量
pthread_mutex_destroy(&mutex);
//销毁条件变量
pthread_cond_destroy(&cond_A_B);
pthread_cond_destroy(&cond_B_C);
pthread_cond_destroy(&cond_C_A);
return 0;
}
结果
本文链接:https://shengto.top/c/cond_routine.html
转载时须注明出处及本声明