#include <stdlib.h>
#include <pthread.h>
#include <iostream>
using namespace std;
int f_count = 5;
pthread_mutex_t f_lock;// = PTHREAD_MUTEX_INITIALIZER;说是静态的用该宏进行初始化
void *test(void *arg)
{
pthread_mutex_lock(&f_lock);
f_count++;
cout<<"child"<<" "<<pthread_self()<<endl;
cout<< f_count<<endl;
pthread_mutex_unlock(&f_lock);
return ((void*)0);
}
int main()
{
pthread_mutex_init(&f_lock, NULL);//动态的用该函数初始化,并通过destroy销毁,试了一下,静态也可以通过该方式初始化
pthread_t test_thread;
pthread_create(&test_thread,NULL, test, NULL);
sleep(1);
pthread_mutex_lock(&f_lock);
f_count++;
cout<<"parent"<<" "<<getpid()<<endl;
cout<< f_count<<endl;
pthread_mutex_unlock(&f_lock);
pthread_mutex_destroy(&f_lock);
return 0;
}
Mutex
最新推荐文章于 2023-01-19 15:59:06 发布