./nptl/pthread_mutex_lock.c:81: __pthread_mutex_lock: Assertion `mutex->__data.__owner == 0' failed

#include<iostream>
#include<pthread.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<errno.h>

using namespace std;
pthread_cond_t taxiarrive = PTHREAD_COND_INITIALIZER;
//int pci = pthread_cond_init(&taxiarrive,NULL);
pthread_mutex_t menarrive = PTHREAD_MUTEX_INITIALIZER;
//int pmi = pthread_mutex_init(&menarrive,NULL);

int mencount = 0;

void* taxicome(void* name){
	cout<<"taxi: "<<(char*)name<<"arrived"<<endl;
	while(1){
		pthread_mutex_lock(&menarrive);
		if(mencount > 0){
			pthread_cond_signal(&taxiarrive);
			pthread_mutex_unlock(&menarrive);}
		pthread_mutex_unlock(&menarrive);}
	pthread_exit((void*)0);}


void* mencome(void* name){
	cout<<(char*)name<<" come,need a taxi!"<<endl;
	pthread_mutex_lock(&menarrive);
	mencount++;
	pthread_cond_wait(&taxiarrive,&menarrive);
	pthread_mutex_unlock(&menarrive);
	cout<<(char*)name<<" now get a taxi!"<<endl;
	pthread_exit((void*)0);}


int main(){
	pthread_t tid[3];
	int iret = 0;
	iret = pthread_create(&tid[0],NULL,taxicome,(void*)(" JACK "));
	if(iret){
		cout<<"线程1创建失败"<<endl;
		return iret;}
	cout<<"time passing by "<<endl;
	sleep(1);
	iret = pthread_create(&tid[1],NULL,mencome,(void*)(" JANE "));
	if(iret){
		cout<<"线程2创建失败"<<endl;
		return iret;}
	cout<<"time passing by "<<endl;
	sleep(1);
	iret = pthread_create(&tid[2],NULL,taxicome,(void*)(" TOM "));
	if(iret){
		cout<<"线程3创建失败"<<endl;
		return iret;}
	cout<<"time passing by "<<endl;
	sleep(1);
	void* retval;
	for(int i = 0;i<3;i++){
		iret = pthread_join(tid[i],&retval);
		if(iret){
			cout<<"线程退出异常"<<endl;
			return iret;}
		cout<<"retval="<<(long)retval<<endl;}
	return 0;}
	

运行结果:

time passing by taxi:  JACK arrived

time passing by 
 JANE  come,need a taxi!
 JANE  now get a taxi!
time passing by 
taxi:  TOM arrived
p5: ../nptl/pthread_mutex_lock.c:81: __pthread_mutex_lock: Assertion `mutex->__data.__owner == 0' failed.
已放弃 (核心已转储)
.......
  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值