#include<pthread.h>
#include<stdio.h>
#include <unistd.h>
class CLock
{
public:
CLock();
virtual ~CLock();
int lock();
int init();
int destroy();
int unlock();
private:
pthread_mutex_t lock_;
};
CLock::CLock()
{
int res =0;
pthread_mutex_t lock_;
}
CLock::~CLock()
{
destroy();
}
int CLock::init()
{
int res =0;
res = pthread_mutex_init(&lock_, NULL);
return res;
}
int CLock::lock ()
{
int res =0;
return pthread_mutex_lock(&lock_);
}
int CLock::unlock()
{
int res =0;
return pthread_mutex_unlock(&lock_);
}
int CLock::destroy()
{
int res =0;
pthread_mutex_destroy(&lock_);
return 0;
}
CLock mylock;
void *pthread_function1(void*)
{
printf("enter the function1:\n");
mylock.lock();
printf("the function1 start work\n");
sleep(5);
mylock.unlock();
printf("the function1 stop work\n");
}
void *pthread_function2(void*)
{
printf("enter the function2:\n");
mylock.lock();
printf("the function2 start work\n");
sleep(5);
mylock.unlock();
printf("the function2 stop work\n");
}