线程mutex小结和使用

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