多线程智能指针编程代码处理,并测试并发结果

多线程智能指针编程代码处理代码示例,纯手写,注:转载注明出处
废话不多说,直接上代码

//#define GLOG_NO_ABBREVIATED_SEVERITIES
#include <Windows.h>
#include <iostream>
#include <memory>
#include <map>
#include <assert.h>
#include <mutex>
#include <queue>
#include <thread>
#include <time.h>

#define  MAX_NUM 1000
#define MAX_THREADS 1000

class Base {
public:
	Base(int id):
		_id(id),
		_flag(false)
	{
		_data = -1;
		_th = NULL;
	}
	~Base() {
		_flag = true;
		printf("~Base:%d\n", _id);
		_th->join();
	}

public:
	int GetData() {
		return _data;
	}

	void HandleData() {
		if (_th == NULL)
		{
			_th = new std::thread([&] {
				DWORD starttick = GetTickCount();
				while (true)
				{
					_data = _id;
					printf("id:%d,data:%d\n", _id, _data);
					if (_flag)
					{
						break;
					}

					Sleep(1000);
				}
			});
		}
	}

	void Stop() {
		_flag = true;
	}

private:
	int _id;
	int _data;
	bool _flag;
	std::thread *_th;
};

typedef std::shared_ptr<Base> BasePtr;
typedef std::map<int, BasePtr> Map;
typedef std::shared_ptr<Map> MapPtr;

class BaseManager{
private:
	BaseManager():_tasks(new Map) {
		
	}
	~BaseManager() {}

public:
	static BaseManager &instance() {
		static BaseManager instance;
		return instance;
	}

	BasePtr UpdateBase(int id) {
		std::lock_guard<std::mutex> lock(_mutex);
		if (!_tasks.unique())
		{
			MapPtr newMapPtr(new Map(*_tasks));
			printf("★★★★★★★★★★★★★★★★★★★★\n");
			_tasks.swap(newMapPtr);
		}
		assert(_tasks.unique());
		BasePtr task = std::make_shared<Base>(id);
		(*_tasks)[id] = task;
		return task;
	}

	void ClearBase() {
		std::lock_guard<std::mutex> lock(_mutex);
		if (!_tasks.unique())
		{
			MapPtr newMapPtr(new Map(*_tasks));
			_tasks.swap(newMapPtr);
		}
		assert(_tasks.unique());
		auto it = _tasks->begin();
		while (it != _tasks->end())
		{
			it->second->Stop();
			it->second.reset();
			it++;	
		}
	}

	void StartBase(int id) {
		BasePtr basePtr = UpdateBase(id);
		basePtr->HandleData();
	}

	int QueryBase(int id) {
		MapPtr task = GetData();
		Map::const_iterator iter = _tasks->find(id);
		if (iter != _tasks->end()) {
			if (iter->second != NULL)
			{
				return iter->second->GetData();
			}
			else
			{
				printf("id:%d destroy.\n", iter->first);
				return -1;
			}
		}
		else {
			return -1;
		}
	}

	MapPtr GetData() {
		std::lock_guard<std::mutex> lock(_mutex);
		return _tasks;
	}
private:
	std::mutex _mutex;
	MapPtr _tasks;
};



int main() {
	std::thread th1([&] {
		int i = 0;
		while (true)
		{
			if (i >= MAX_THREADS)
			{
				i = 0;
			}
			BaseManager::instance().StartBase(i);
			Sleep(20);
			i++;
		}

	});

	std::thread th([&] {
		while (true)
		{
			for (int i = 0; i < MAX_THREADS; i++)
			{
				BaseManager::instance().QueryBase(i);
			}
			Sleep(500);
		}
	});

	BaseManager::instance().ClearBase();

	while (true)
	{
		Sleep(1000);
	}
	system("pause");
	return 0;
}

未完待续。。。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值