linux线程同步简单示例
作者:互联网
#include<stdio.h>
#include<pthread.h>
#include<stdlib.h>
//int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg);
//int pthread_join(pthread_t thread, void **retval);
//void pthread_exit(void *retval);
//函数模型
int get_data = 0;
pthread_mutex_t mutex;//定义锁
pthread_cond_t cond;//定义控制信号
void *func1(void *arg)
{
while(1) { pthread_cond_wait(&cond,&mutex);//等待在mutex这把锁上的信号到来才能解除阻塞,否则一直阻塞下去 sleep(1); printf("get_data =%d\n",get_data); printf("t1 run================\n"); get_data = 0; sleep(1); } // pthread_exit(); } void *func2(void *arg)//指针的函数 返回指针 { pthread_mutex_lock(&mutex);//加锁 while(1) { sleep(1); printf("t2 get_data = %d\n",get_data); get_data++; if(get_data == 3) {//全局变量到3发送信号func1执行 pthread_cond_signal(&cond); pthread_mutex_unlock(&mutex);//解锁 sleep(1);//睡一秒让出cpu时间片不让t2线程抢到执行权 } } } void *func3(void *arg) { // pthread_mutex_lock(&mutex); // pthread_mutex_unlock(&mutex); // pthread_exit(); }
int main() { int ret1 = 0; int ret2 = 0; int ret3 = 0; int param = 100; pthread_t t1;//定义线程变量 pthread_t t2; pthread_t t3; pthread_cond_init(&cond,NULL);//初始化信号 pthread_mutex_init(&mutex,NULL);//初始化锁 ret1 = pthread_create(&t1,NULL,func1,(void*)¶m);//创建线程 if(ret1 == 0) { // printf("main create to t1 success\n"); } ret2 = pthread_create(&t2,NULL,func2,(void*)¶m); if(ret2 == 0) { // printf("main create to t2 success\n"); } ret3 = pthread_create(&t3,NULL,func3,(void*)¶m); if(ret3 == 0) { // printf("main create to t3 success\n"); }
// printf("main %ld\n",(unsigned long)pthread_self()); pthread_join(t1,NULL);//主函数线程执行完等这些创建的线程执行完才能退出 pthread_join(t2,NULL);//不设置的可能会导致主线程执行完释放,其他线程无法正常执行 pthread_join(t3,NULL); pthread_mutex_destroy(&mutex);//销毁锁 pthread_cond_destroy(&cond);//销毁信号 return 0; }
while(1) { pthread_cond_wait(&cond,&mutex);//等待在mutex这把锁上的信号到来才能解除阻塞,否则一直阻塞下去 sleep(1); printf("get_data =%d\n",get_data); printf("t1 run================\n"); get_data = 0; sleep(1); } // pthread_exit(); } void *func2(void *arg)//指针的函数 返回指针 { pthread_mutex_lock(&mutex);//加锁 while(1) { sleep(1); printf("t2 get_data = %d\n",get_data); get_data++; if(get_data == 3) {//全局变量到3发送信号func1执行 pthread_cond_signal(&cond); pthread_mutex_unlock(&mutex);//解锁 sleep(1);//睡一秒让出cpu时间片不让t2线程抢到执行权 } } } void *func3(void *arg) { // pthread_mutex_lock(&mutex); // pthread_mutex_unlock(&mutex); // pthread_exit(); }
int main() { int ret1 = 0; int ret2 = 0; int ret3 = 0; int param = 100; pthread_t t1;//定义线程变量 pthread_t t2; pthread_t t3; pthread_cond_init(&cond,NULL);//初始化信号 pthread_mutex_init(&mutex,NULL);//初始化锁 ret1 = pthread_create(&t1,NULL,func1,(void*)¶m);//创建线程 if(ret1 == 0) { // printf("main create to t1 success\n"); } ret2 = pthread_create(&t2,NULL,func2,(void*)¶m); if(ret2 == 0) { // printf("main create to t2 success\n"); } ret3 = pthread_create(&t3,NULL,func3,(void*)¶m); if(ret3 == 0) { // printf("main create to t3 success\n"); }
// printf("main %ld\n",(unsigned long)pthread_self()); pthread_join(t1,NULL);//主函数线程执行完等这些创建的线程执行完才能退出 pthread_join(t2,NULL);//不设置的可能会导致主线程执行完释放,其他线程无法正常执行 pthread_join(t3,NULL); pthread_mutex_destroy(&mutex);//销毁锁 pthread_cond_destroy(&cond);//销毁信号 return 0; }
标签:示例,int,void,linux,mutex,pthread,线程,get,cond 来源: https://www.cnblogs.com/ightningmcqueen/p/16660324.html