Linux多线程 -互斥锁 pthread_mutex

/* review sem for pthread && pthread_mutex
 * c++ virtual function use
 * pthread_mutex 使用场景:多线程操作global变量&&share function时(互斥)
 * 
*/
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <pthread.h>
#include <unistd.h>
#include <semaphore.h> //sem add must
#include <fstream>
#include <stdlib.h>
#include <string.h>
using namespace std;

pthread_mutex_t log_lock;
sem_t sem; //Unsigned long

class Thread
{
	public:
		virtual void set();

		Thread()
		{
			pthread_mutex_init(&log_lock,NULL);
			sem_init(&sem,0,0);
		};

		~Thread()
		{
			pthread_mutex_destroy(&log_lock);
			sem_destroy(&sem);
			
		};
};

void s_thread_call(char *msg)
{
	int i = 0 ;
	for(i=0;i < 10;i++)
	{
		//cout<<"rand() = "<<rand()<<endl;
		//rand();
		
	}
	cout<<"s_thread_call ="<<msg<<endl;
}
//两个线程都有result
void* cb1(void  *arg)
{
	int i = 0;
	char *tp = (char*)arg; //强制转换
	cout<< "fuct_par1 =  "<< tp <<endl;
	while(1)
	{
		pthread_mutex_lock(&log_lock);
		s_thread_call(tp);
		pthread_mutex_unlock(&log_lock); 
		sleep(1); //作用是 如果不加的话 另一个线程没有机会 invoke s_thread_call
	}

}

void* cb2(void  *arg)
{
	int i = 0;
	char *tp = (char*)arg;
	cout<< "fuct_par2 =  "<< tp <<endl;
	while(1)
	{
		printf( "log_lock b  = %d\n",log_lock);
		pthread_mutex_lock(&log_lock); // 作用是 多线程时只允许 一个线程访问 这段代码
		s_thread_call(tp);
		printf( "log_lock l  = %d\n",log_lock);
		pthread_mutex_unlock(&log_lock); //unlock 后 log_lock 值为零
		printf( "log_lock ul = %d\n",log_lock); 
		sleep(1);
	}

}

int main(int argc ,char* argv[])
{
	char* msg1 = "thread 1 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>";
	char* msg2 = "thread 2 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>";

	pthread_t td1,td2;
	int ret1 = -1 ,ret2 = -1;
	ret2 = pthread_create(&td2,NULL,cb2,msg2);
	ret1 = pthread_create(&td1,NULL,cb1,msg1);
	
	if(ret1 && ret2)
	{
		cout<<"creat phtread failed "<<endl;
		exit(0);
	}
	
	ret2 = pthread_join(td2,NULL);
	ret1 = pthread_join(td1,NULL);

	if(ret1 && ret2)
	{
		cout<<"join phtread failed "<<endl;
		exit(0);
	}
	return 0;
}

运行结果:

log_lock b  = 133892288
s_thread_call =thread 2 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
log_lock l  = 133892288
log_lock ul = 0
fuct_par1 =  thread 1 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
s_thread_call =thread 1 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
log_lock b  = 123780752
s_thread_call =thread 1 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
s_thread_call =thread 2 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
log_lock l  = 133892288
log_lock ul = 0
s_thread_call =thread 1 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
log_lock b  = 123780752
s_thread_call =thread 2 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
log_lock l  = 133892288
log_lock ul = 0
s_thread_call =thread 1 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
log_lock b  = 123780752
s_thread_call =thread 2 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
log_lock l  = 133892288
log_lock ul = 0
log_lock b  = 123780752
s_thread_call =thread 1 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
s_thread_call =thread 2 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
log_lock l  = 133892288
log_lock ul = 0
s_thread_call =thread 1 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
log_lock b  = 123780752
s_thread_call =thread 2 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
log_lock l  = 133892288
log_lock ul = 0
s_thread_call =thread 1 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
log_lock b  = 123780752
s_thread_call =thread 2 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
log_lock l  = 133892288
log_lock ul = 0

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值