linux thread mutex example

#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <pthread.h>

//-lpthread 

#define PTHREAD_MUTEX_RECURSIVE 1

pthread_mutex_t my_mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;

void my_unlock_mutex(void)
{
  int retMutex;

  retMutex =  pthread_mutex_unlock(&my_mutex);
  if (retMutex) printf("fail - %s", strerror(retMutex));

}


void my_lock_mutex(void)
{

  int retMutex;

  retMutex = pthread_mutex_lock(&my_mutex);
  if (retMutex) printf("fail - %s", strerror(retMutex));


}

void my_init_mutex(void)
{

  int error;
  pthread_mutexattr_t mutex_attr;

  pthread_mutexattr_init(&mutex_attr);

#ifdef PTHREAD_MUTEX_RECURSIVE
  pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_RECURSIVE);
#else
  pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_RECURSIVE_NP);
#endif

  error = pthread_mutex_init(&my_mutex, &mutex_attr);
  if (error) printf("fail - %s", strerror(error));
  pthread_mutexattr_destroy(&mutex_attr);


}

int g_iValue =0;

 pthread_t thread_id1;
 pthread_t thread_id2;

void *test1(void *)
{
	my_lock_mutex();
	for(int i=0;i<10;i++)
	{
		g_iValue = 1;
		printf("value =%d thead =%lu %s\n", g_iValue,pthread_self(),(pthread_self()==thread_id1)?"thread1":"error1");
		sleep(5);
	}
	my_unlock_mutex();


}

void *test2(void *)
{
	my_lock_mutex();
	for(int i=0;i<10;i++)
	{
		g_iValue = 2;
		printf("value =%d thead =%lu %s\n", g_iValue,pthread_self(),pthread_self()==thread_id2?"thread2":"error2");
		sleep(1);
	}
	my_unlock_mutex();


}


int main()
{


   	int ret;
	my_init_mutex();
			
	ret = pthread_create(&thread_id1, NULL, test1, NULL);
	ret = pthread_create(&thread_id2, NULL, test2, NULL);

	


	pthread_join(thread_id1,NULL);//calling thread wait for thread_id1 terminlated 
	pthread_join(thread_id2,NULL);//calling thread wait for thread_id1 terminlated 

	printf("thread_id1 =%lu,thread_id2=%lu\n",thread_id1,thread_id2);

	return 0;
	



}

-> g++ -o my_lock -lpthread lock.cpp 
-> ./my_lock
value =1 thead =3086449568 thread1
value =1 thead =3086449568 thread1
value =1 thead =3086449568 thread1
value =1 thead =3086449568 thread1
value =1 thead =3086449568 thread1
value =1 thead =3086449568 thread1
value =1 thead =3086449568 thread1
value =1 thead =3086449568 thread1
value =1 thead =3086449568 thread1
value =1 thead =3086449568 thread1
value =2 thead =3075959712 thread2
value =2 thead =3075959712 thread2
value =2 thead =3075959712 thread2
value =2 thead =3075959712 thread2
value =2 thead =3075959712 thread2
value =2 thead =3075959712 thread2
value =2 thead =3075959712 thread2
value =2 thead =3075959712 thread2
value =2 thead =3075959712 thread2
value =2 thead =3075959712 thread2
thread_id1 =3086449568,thread_id2=3075959712


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值