pthread_cond_signal例子

demo

#include "phtreadsignal.h"#include
#include 
#include
#include using namespace std;pthread_mutex_t mutexm2 = PTHREAD_MUTEX_INITIALIZER;/*初始化互斥锁*/
pthread_cond_t cond2 = PTHREAD_COND_INITIALIZER;//pthread_cond_t  _cond [1] ;#define USE_LOCK 1struct member2 {int index;int end;pthread_t t;
};void enq(int index,pthread_t t);
void signal();void *threadms1(member2* m) {member2* m2 = m;int num1 = m2->end;int index = m2->index;cout << "[[[[[[[[[[[[[[[index=" << index << endl;pthread_t t = m2->t;
//    if(index >-1 && index <1) {if(index == 0) {cout << "============================exit==============================" << endl;
//        exit(1);}
//    cout << "---------threadm:" << index << "  num1=" << num1 << " end=" << num1 << endl;if(USE_LOCK){cout << "***************before lock:" << index << "  num1=" << num1 << endl;pthread_mutex_lock(&mutexm2);}if(index>1) {
//        cout << "---------start enq:" << index << "  num1=" << num1 << " end=" << num1 << endl;enq(index,NULL);cout << "---------after enq:" << index << "  num1=" << num1 << " end=" << num1 << endl;}while (num1 >0) {cout << "###########threadm:" << index << "  num1=" << num1 << " end=" << num1 << endl;sleep(1);num1--;}if(USE_LOCK) {cout << "---------signal:" << index << "  num1=" << num1 << endl;
//        pthread_cond_signal()必须要放在pthread_mutex_lock() 和pthread_mutex_unlock() 之间signal();pthread_mutex_unlock(&mutexm2);cout << "---------unlock:" << index << "  num1=" << num1 << endl;
//        signal(); // 锁之后释放if(t!=NULL) {
//            status = pthread_cond_wait (&_cond[_cur_index], _mutex) ;
//            pthread_cond_signal(&_cond[0]); //}}}void enq(int index,pthread_t t) {
//    int status = pthread_cond_wait (&_cond[0], mutexm2) ;
//    int status = pthread_cond_wait (&_cond[0], mutexm2) ;
//    int status = pthread_cond_wait(reinterpret_cast(&_cond), mutexm2) ;cout << "before enq wait index=" << index << endl;int status = pthread_cond_wait(&cond2, &mutexm2) ;cout << "after enq wait index=" << index << " ,status=" << status << endl;}void *mysignal(member2* m) {int num = 10;
//    while(num>0) {sleep(30);pthread_mutex_lock(&mutexm2);cout << "mysignal" << ",wake up thread num="<< num << endl;signal();pthread_mutex_unlock(&mutexm2);//        num--;
//    }}void signal() {cout << " dequeue dequeue dequeue ============== " << endl;int status = pthread_cond_signal(&cond2) ;cout << "dequeue status=" << status << endl;
}int main(){pthread_t t_a;pthread_t t_b;pthread_t t_c;pthread_t t_d;pthread_t t_e;int end = 5;struct member2 *a = (struct member2 *)malloc(sizeof(struct member2));// 清0memset(a,0,sizeof(struct member2));a->index =1;a->end=end;struct member2 *b = (struct member2 *)malloc(sizeof(struct member2));memset(b,0,sizeof(struct member2));b->index =2;b->end=end;struct member2 *c = (struct member2 *)malloc(sizeof(struct member2));memset(c,0,sizeof(struct member2));c->index =3;c->end=end;struct member2 *d = (struct member2 *)malloc(sizeof(struct member2));memset(d,0,sizeof(struct member2));d->index =4;d->end=end;struct member2 *e = (struct member2 *)malloc(sizeof(struct member2));memset(e,0,sizeof(struct member2));e->index =5;e->end=end;pthread_create(&t_a, NULL, reinterpret_cast<void *(*)(void *)>(threadms1), (void*)a);pthread_create(&t_b, NULL, reinterpret_cast<void *(*)(void *)>(threadms1), (void*)b);pthread_create(&t_c, NULL, reinterpret_cast<void *(*)(void *)>(threadms1), (void*)c);pthread_create(&t_d, NULL, reinterpret_cast<void *(*)(void *)>(threadms1), (void*)d);pthread_create(&t_e, NULL, reinterpret_cast<void *(*)(void *)>(threadms1), (void*)e);pthread_t t_signal;pthread_create(&t_signal, NULL, reinterpret_cast<void *(*)(void *)>(mysignal), NULL);//    pthread_join(t_a,NULL);//wait a_b thread end
//    pthread_join(t_b,NULL);//wait a_b thread end
//    pthread_join(t_c,NULL);//wait a_b thread end
//    pthread_join(t_d,NULL);//wait a_b thread end
//    pthread_join(t_e,NULL);//wait a_b thread end//    pthread_cond_destroy (&_cond[_cur_index]) ;
//    int status = pthread_cond_init    (&_cond[0], NULL);sleep(1000);return 1;
}

sleep

# 单位second
unsigned int __cdecl sleep (unsigned int);

pthread_cond_wait

pthread_mutex_lock(&mutexm2);
pthread_cond_wait(&cond2, &mutexm2);
pthread_mutex_unlock(&mutexm2);

pthread_cond_signal

pthread_mutex_lock(&mutexm2);
pthread_cond_signal(&cond2);
pthread_mutex_unlock(&mutexm2);


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部