恒柔 2016-07-25 14:18 采纳率: 100%
浏览 1211

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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 vue中我代理了iframe,iframe却走的是路由,没有显示该显示的网站,这个该如何处理
    • ¥15 操作系统相关算法中while();的含义
    • ¥15 CNVcaller安装后无法找到文件
    • ¥15 visual studio2022中文乱码无法解决
    • ¥15 关于华为5g模块mh5000-31接线问题
    • ¥15 keil L6007U报错
    • ¥15 webapi 发布到iis后无法访问
    • ¥15 初学者如何快速上手学习stm32?
    • ¥15 如何自动更换布娃娃图片上的衣服
    • ¥15 心理学eprime编程