读书人

线程mutex小结和使用

发布时间: 2013-01-23 10:44:50 作者: rapoo

线程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;}


读书人网 >编程

热点推荐