linux 多线程加互斥锁没用

程序执行结果: girl(0xb707fb70) withdraw 1000.000000 from Account 101
boy(0xb7880b70) withdraw 1000.000000 from Account 101
account balance:0.000000
几个程序如下:
account.c程序源码:
#include "account.h"
#include
#include
#include

//create Account
Account* create_account(int code,double balance){
Account a = (Account)malloc(sizeof(Account));
assert(a !=NULL);
a->code = code;
a->balance = balance;
//对互斥锁初始化
pthread_mutex_init(&a->mutex,NULL);
return a;
}
//destroy Account
void destroy_account(Account *a){
assert(a != NULL);
pthread_mutex_destroy(&a->mutex);
free(a);
}
//qu money
double withdraw(Account *a,double amt){
assert(a != NULL);
pthread_mutex_lock(&a->mutex);
if(amt < 0 || amt > a->balance){

pthread_mutex_unlock(&a->mutex);
return 0.0;
}

 double balance = a->balance;
 sleep(1);
 balance -= amt;
 a->balance = balance;
 pthread_mutex_unlock(&a->mutex);
 return amt;

}
//cun money
double deposit(Account *a,double amt){
assert(a !=NULL);
pthread_mutex_lock(&a->mutex);
if(amt < 0){
pthread_mutex_unlock(&a->mutex);
return 0.0;
}

 double balance = a->balance;
 sleep(1);
 balance += amt;
 a->balance = balance;

pthread_mutex_unlock(&a->mutex);
 return amt;

}
//see money
double get_balance(Account *a){
assert(a != NULL);

pthread_mutex_lock(&a->mutex);
 double balance = a->balance;
pthread_mutex_unlock(&a->mutex);
 return balance;

}
account.h头文件源码:
#ifndef ACCOUNT_H
#define ACCOUNT_H
#include
typedef struct
{
int code;
double balance;
//定义互斥锁
pthread_mutex_t mutex;
}Account;
//create Account
extern Account* create_account(int code,double balance);
//destroy Account
extern void destroy_account(Account *a);
//qu money
extern double withdraw(Account *a,double amt);
//cun money
extern double deposit(Account *a,double amt);
//see money
extern double get_account(Account *a);
#endif

调用程序account-test.c源码:
#include "account.h"
#include
#include
#include
#include

typedef struct{
char name[20];
Account account;
double amt;
}OperArg;
//取款操作线程运行函数
void
withdraw_fn(void arg){
OperArg *oa = (OperArg
)arg;
double amt = withdraw(oa->account,oa->amt);
printf("%8s(0x%lx) withdraw %f from Account %d\n",oa->name,pthread_self(),
amt,oa->account->code);
return (void*)0;
}

//存款线程运行函数
void* deposit_fn(void *arg){

OperArg *oa = (OperArg*)arg;
double amt = deposit(oa->account,oa->amt);
printf("%8s(0x%lx) deposit %f from Account %d\n",oa->name,pthread_self(),
        amt,oa->account->code);
return (void*)0;

}

int main(void){
int err;
pthread_t boy,girl;
Account *a = create_account(101,10000);
OperArg o1,o2;
strcpy(o1.name,"boy");
o1.account = a;
o1.amt = 1000;
// OperArg o1 = {"boy",a,1000};

strcpy(o2.name,"girl");
o2.account = a;
o2.amt = 1000;

if((err = pthread_create(&boy,NULL,withdraw_fn,(void*)&o1)) != 0){
    perror("pthread create error");
}
if((err = pthread_create(&girl,NULL,withdraw_fn,(void*)&o2)) != 0){
    perror("pthread create error");
}
pthread_join(boy,NULL);
pthread_join(girl,NULL);
printf("account balance:%f\n",get_balance(a));
destroy_account(a);
return 0;

}
请问为什么互斥锁不起作用啊?怎么解决啊,多谢!

0个回答

Linux多线程 |互斥锁|条件变量
linux多线程与互斥锁
----------------------biu~biu~biu~~~在下问答机器人小D,这是我依靠自己的聪明才智给出的答案,如果不正确,你来咬我啊!

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问