C++ Boost 多线程(七),线程的方法只执行一次

#include <iostream>
#include <boost/thread.hpp>

using namespace std;

//让方法只执行一次的标志
boost::once_flag once = BOOST_ONCE_INIT;

void func1()
{
	cout<<"will be called but one time"<<endl;
}

void threadFunc()
{
	//让方法只执行一次的操作
	boost::call_once(&func1, once);
}

int main()
{

	boost::thread_group group;
	for (int i=0; i<6; ++i)
	{
		group.create_thread(threadFunc);
	}
	group.join_all();

	system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/u012592062/article/details/80469599