栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

【C++多线程】银行多人转账模拟

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【C++多线程】银行多人转账模拟

文章目录
      • 一、题目要求
      • 二、代码实现
      • 三、使用线程并发库

一、题目要求

使用C++的线程并发库,实现并模拟多人在线同时转账的过程,确保转账不能出现差错。

例如:
Account A(“xiaoming”, 1000);
Account B(“zhangqiang”, 2000);
Account C(“zq”, 1500);

B->A 200    A 1200 B 1800
B->C 500    B 1300 C 2000
C->A 300    A 1500 C 1700
二、代码实现
#if 1
#include
#include
#include
#include
using namespace std;

std::mutex mtx;
std::condition_variable cv;

class Account
{
public:
	Account() :name(), money(0) {}
	Account(const string& na, int my) : name(na), money(my) {}
	~Account() = default;
	const int GetMoney() const
	{
		return money;
	}
	void SetMoney(int m)
	{
		money = m;
	}
	const string& GetName()const
	{
		return name;
	}
private:
	string name;
	int money;
};

class Bank
{
public:
	~Bank() {}
	static Bank& GetSingleBank()
	{
		return bank;
	}
	//A转给B money
	void TransAccounts(Account& A, Account& B)
	{
		unique_lock lock(mtx);
		while (map.count(A.GetName()) > 0 || map.count(B.GetName()) > 0)
		{
			cv.wait(lock);
		}
		map[A.GetName()] = A.GetMoney();
		map[B.GetName()] = B.GetMoney();
	}
	//清除交易完成的账户
	void ClearAccount(Account& A, Account& B)
	{
		unique_lock lcok(mtx);
		map.erase(A.GetName());
		map.erase(B.GetName());
		cv.notify_all();
	}
private:
	Bank() = default;
	Bank& operator=(const Bank& b) = delete;
	static Bank bank;

	std::map map;
};
//单例:类外初始化
Bank Bank::bank;

//线程函数
void ThreadFun(Account& A, Account& B, int money)
{
	if (money <= 0) return;
	Bank& bank = Bank::GetSingleBank();
	bank.TransAccounts(A, B);
	if (A.GetMoney() >= money)
	{
		A.SetMoney(A.GetMoney() - money);
		B.SetMoney(B.GetMoney() + money);
	}
	else
	{
		cout << A.GetName() << "->" << B.GetName() << "交易失败" << endl;
	}
	bank.ClearAccount(A, B);
}

int main()
{
	Account A("xiaoming", 1000);
	Account B("zhangqiang", 2000);
	Account C("zq", 1500);
	thread tha(ThreadFun, std::ref(B), std::ref(A), 200); //A 1200 B 1800
	thread thb(ThreadFun, std::ref(B), std::ref(C), 500); //B 1300 C 2000
	thread thc(ThreadFun, std::ref(C), std::ref(A), 300); //A 1500 C 1700

	tha.join();
	thb.join();
	thc.join();
	//A 1500 B 1300 C 1700
	cout << A.GetName() << ": "<< A.GetMoney()<< endl;
	cout << B.GetName() << ": "<< B.GetMoney()<< endl;
	cout << C.GetName() << ": " << C.GetMoney() << endl;
	
	return 0;
}
#endif

三、使用线程并发库
#include
#include
using namespace std;

class Account
{
public:
	Account() :name(), money(0) {}
	Account(const string& na, int my) : name(na), money(my) {}
	~Account() = default;
	const int GetMoney() const
	{
		return money;
	}
	void SetMoney(int m)
	{
		money = m;
	}
	const string& GetName()const
	{
		return name;
	}
private:
	string name;
	int money;
};

#if 1
mutex mtx1;
mutex mtx2;
void ThreadFun(Account& A, Account& B, int money)
{
	unique_lock lock1(mtx1, defer_lock);
	unique_lock lock2(mtx2, defer_lock);
	lock(lock1, lock2);

	if (A.GetMoney() >= money)
	{
		A.SetMoney(A.GetMoney() - money);
		B.SetMoney(B.GetMoney() + money);
	}
}

int main()
{
	Account A("xiaoming", 1000);
	Account B("zhangqiang", 2000);
	Account C("zq", 1500);
	thread tha(ThreadFun, std::ref(B), std::ref(A), 200); //A 1200 B 1800
	thread thb(ThreadFun, std::ref(B), std::ref(C), 500); //B 1300 C 2000
	thread thc(ThreadFun, std::ref(C), std::ref(A), 300); //A 1500 C 1700

	tha.join();
	thb.join();
	thc.join();
	//A 1500 B 1300 C 1700
	cout << A.GetName() << ": " << A.GetMoney() << endl;
	cout << B.GetName() << ": " << B.GetMoney() << endl;
	cout << C.GetName() << ": " << C.GetMoney() << endl;

	return 0;
}
#endif

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/853601.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号