#include<iostream>
#include<pthread.h>
using namespace std;
class MutexLock
{
private:
pthread_mutex_t mutex;
//pthread_cond_t cond;
public:
MutexLock()
{
cout<<"MutexLock()"<<endl;
pthread_mutex_init(&mutex,NULL);
}
~MutexLock()
{
cout<<"~Mutexlock()"<<endl;
pthread_mutex_destroy(&mutex);
}
void lock()
{
//cout<<"Lock()"<<endl;
pthread_mutex_lock(&mutex);
}
void unlock()
{
//cout<<"unlock()"<<endl;
pthread_mutex_unlock(&mutex);
}
};
//没有互斥锁的时候的结果
|
int num = 0;
void *func(void* p)
{
MutexLock *mutp = (MutexLock*)p;
for(int idx=0;idx!=5e8;idx++)
{
(*mutp).lock();
num++;
(*mutp).unlock();
}
pthread_exit(NULL);
}
int main(void)
{
MutexLock mut;
pthread_t pth;
pthread_create(&pth,NULL,func,(void*)&mut);
for(int idx=0;idx!=5e8;idx++)
{
mut.lock();
num++;
mut.unlock();
}
pthread_join(pth,NULL);
cout<<"num="<<num<<endl;
}
//用了互斥锁的时候的结果
|