首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

线程mutex小结和使用

2013-01-23 
线程mutex总结和使用1.mutex常见的类型1.1 pthread_mutex_t#include iostreamusing namespace stdint g

线程mutex总结和使用
1.mutex常见的类型
1.1 pthread_mutex_t

#include <iostream>using namespace std;int gNum = 0;pthread_mutex_t m;void * threadProc(void * param){        pthread_mutex_lock(&m);        for (int i = 0; i < 10; ++i){                sleep(1);                cout << (int)param << "gNum = " << ++gNum << endl;        }        pthread_mutex_unlock(&m);}int main(){        pthread_t tid1;        pthread_t tid2;        pthread_mutex_init(&m, NULL);        pthread_create(&tid1, NULL, threadProc, (void *)1);        pthread_create(&tid2, NULL, threadProc, (void *)2);        pthread_mutex_destroy(&m);        pthread_join(tid1, NULL);        pthread_join(tid2, NULL);        return 0;}


热点排行