C++ 11 线程调用类的成员函数解决办法

在C++中,_beginthreadex 创建线程是很麻烦的。要求入口函数必须是类的静态函数。

通常,可以采用thunk,或者模板来实现。

因C++ 11中引入了 std::async ,可以很好的解决这个问题了。

值得注意的是,在循环中 std::async 创建线程,我试了好多次总是失败,后来看老外的代码,采用 std::move解决了问题。

具体见实现代码。

 

// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。

#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <functional>
#include <process.h>
#include <thread>
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <iterator>
#include <future>
#include <list>
#include <sstream>
using namespace std;


typedef UINT(WINAPI* THREADPROC)(LPVOID);


class XTest
{
public:
	XTest() {};
	~XTest() {};
	
	UINT XTest::RunLoop(LPVOID obj)
	{
		int count = 0;
		for (int i = 0; i < 5; i++)
		{
			Sleep(100);
			std::ostringstream ostr;
			ostr << "线程" << (INT)obj << "   静态执行次数 [" << count++ << "] -RunLoop- " << std::endl;
			cout << ostr.str().c_str() << std::endl;
		}
		return 0;
	}

	UINT XTest::XFH()
	{
		//  问题就出在这里,_beginthreadex不能调用类的成员函数,须静态的才行。
		//int rt = _beginthreadex(NULL, 0, (THREADPROC) [this](LPVOID) { return this->RunLoop(0); }, 0, 0, NULL);
		// auto pppp = (&XTest::RunLoop);
		// ULONG_PTR* kkkk = (ULONG_PTR*)&pppp;
		std::list<std::future<UINT> > lk;
		for (int i =0 ; i<8 ; i++)
		{
			//auto __p = std::async(std::launch::async , [this, i]() {return this->RunLoop((LPVOID)i); });
			auto __p = std::async(std::launch::async, &XTest::RunLoop, this, (LPVOID)i);
			// __p.wait(); 不可以调用。否则会变成同步了。
			lk.push_back(std::move(__p));  // 这里必须要用move,否则就会变成同步了。。
			//auto k = std::thread([this , i]() {this->RunLoop((LPVOID)i); });
			//k.detach();
		}
		// 此行代码用于等线程结束,会阻塞主线程。
		for (auto &e : lk)
		{
			e.wait();
		}
		cout << "等线程结束" << endl;
		Sleep(1000);
		return 0;
	}
};

int main()
{
	auto mmm = new XTest;
	mmm->XFH();
	cout << "------";
	getchar();
	return 0;
}



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值