无锡上网站建设,Wordpress和drupal开发,什么网站备案比较快,旅游管理网站业务模块条件变量
条件变量本身不是锁#xff0c;但是它可以造成线程阻塞。通常于互斥锁配合使用。给多线程提供一个会和的场合。
使用互斥量保护共享数据使用条件变量可以造成线程阻塞#xff0c;等待某个条件的发生#xff0c;当条件满足的时候解除阻塞。
条件变量的两个动作但是它可以造成线程阻塞。通常于互斥锁配合使用。给多线程提供一个会和的场合。
使用互斥量保护共享数据使用条件变量可以造成线程阻塞等待某个条件的发生当条件满足的时候解除阻塞。
条件变量的两个动作
条件不满足阻塞线程条件满足通知阻塞的线程解除阻塞 相关函数
pthread_cond_t cond 定义一个cond条件变量
int pthread_cond_init(pthread_cond_t *restrict cond, const pthread_condattr_t *restrict attr);
函数描述初始化条件变量
cond 条件变量 attr 条件变量属性设NULL
函数返回值:成功返回0失败返回错误号
int pthread_cond_destroy(pthread_cond_t *cond);
函数描述销毁一个条件变量
int pthread_cond_wait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex);
函数描述条件不满足引起线程阻塞并解锁 条件满足解除条件阻塞并加锁
函数参数cond-条件变量 mutex-互斥锁
int pthread_cond_signal(pthread_cond_t *cond);
函数描述:唤醒至少一个阻塞在该条件变量cond上的线程
函数参数条件变量
函数返回值成功返回0失败返回错误号
生产者与消费者模型
#includestdio.h
#includestring.h
#includestdlib.h
#includesys/types.h
#includeunistd.h
#includepthread.h
//定义一个链表
typedef struct node
{int data;struct node* next;
}node;
//定义一个头节点
node *phead;
//定义一个互斥锁变量
pthread_mutex_t mutex;
//定义一个条件变量
pthread_cond_t cond;
void *producer(void *arg)
{srand(time(NULL));while(1){
//生成一个新的节点node *pNodeNULL;pNode(node*)malloc(sizeof(node));if(pNodeNULL){printf(malloc error);exit(-1);}
//加锁pthread_mutex_lock(mutex);pNode-datarand()%100;//随机生成数printf(p:[%d]\n,pNode-data);
//头插法pNode-nextphead;pheadpNode;
//解锁pthread_mutex_unlock(mutex);
//唤醒至少一个线程pthread_cond_signal(cond);sleep(1);//防止生成过快导致内存不足}
}
void *consumer(void *arg)
{while(1){pthread_mutex_lock(mutex);//加锁if(pheadNULL){
//如果头节点为空那么阻塞并解锁
//如果头节点不为空接收到pthread_cond_signal的唤醒解除阻塞并加锁pthread_cond_wait(cond,mutex);}node*pNodephead;printf(c:[%d]\n,pNode-data);
//头节点移动pheadphead-next;
//释放当前节点free(pNode);pNodeNULL;
//解锁pthread_mutex_unlock(mutex);sleep(2);}
}
int main()
{pthread_mutex_init(mutex,NULL);//初始化互斥锁pthread_cond_init(cond,NULL);//初始化条件变量pthread_t thread1;pthread_t thread2;int retpthread_create(thread1,NULL,producer,NULL);if(ret!0){printf(pthread_create1 error:[%s]\n,strerror(ret));return -1;}retpthread_create(thread2,NULL,consumer,NULL);if(ret!0){printf(pthread_create2 error:[%s]\n,strerror(ret));return -1;}
//阻塞等待线程结束pthread_join(thread1,NULL);pthread_join(thread2,NULL);
//销毁pthread_mutex_destroy(mutex);pthread_cond_destroy(cond);return 0;
} 多线程core掉的情况
假如只有一个生产者生产了一个节点此时会调用pthread_cond_signal通知消费者线程此时若有多个消费者被唤醒了则最终只有一个消费者获得锁然后进行消费此时会将head置为NULL,然后其他被唤醒的消费者线程会有一个获得锁然后读取的head的内容会core掉。
#includestdio.h
#includestring.h
#includestdlib.h
#includesys/types.h
#includeunistd.h
#includepthread.h
typedef struct node
{int data;struct node* next;
}node;
node *phead;
pthread_mutex_t mutex;
pthread_cond_t cond;
void *producer(void *arg)
{srand(time(NULL));while(1){node *pNodeNULL;pNode(node*)malloc(sizeof(node));if(pNodeNULL){printf(malloc error);exit(-1);}pthread_mutex_lock(mutex);pNode-datarand()%100;printf(p:[%d]\n,pNode-data);pNode-nextphead;pheadpNode;pthread_mutex_unlock(mutex);pthread_cond_signal(cond);sleep(1);}
}
void *consumer(void *arg)
{int n;while(1){n*(int *)arg;pthread_mutex_lock(mutex);if(pheadNULL){pthread_cond_wait(cond,mutex);}if(pheadNULL){pthread_mutex_unlock(mutex);//先解锁因为pthread_cond_wait会加一个锁continue;}node*pNodephead;printf(c[%d]:[%d]\n,n,pNode-data);pheadphead-next;free(pNode);pNodeNULL;pthread_mutex_unlock(mutex);sleep(1);}
}
int main()
{int i0;int arr[5];pthread_mutex_init(mutex,NULL);pthread_cond_init(cond,NULL);pthread_t thread1;pthread_t thread2;int retpthread_create(thread1,NULL,producer,NULL);if(ret!0){printf(pthread_create1 error:[%s]\n,strerror(ret));return -1;}for(;i5;i)//创建5个消费者子线程{arr[i]i;retpthread_create(thread2,NULL,consumer,arr[i]);if(ret!0){printf(pthread_create2 error:[%s]\n,strerror(ret));return -1;}}pthread_join(thread1,NULL);pthread_join(thread2,NULL);pthread_mutex_destroy(mutex);pthread_cond_destroy(cond);return 0;
}