Welcome to collectivesolver - Programming & Software Q&A. A website you can trust. All programs tested and works. Contact: aviboots(AT)netvision.net.il
Hostgator 1 Penny

BlueHost Web Hosting

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

getflywheel wordpress hosting


Distribute your Resume to 500+ Job Boards & Recruiters


7,586 questions

10,629 answers

573 users

How to joining threads in C++

1 Answer

0 votes
#include <iostream>
#include <thread>
#include <algorithm>
#include <vector>

using std::cout;
using std::endl;
using std::thread;
using std::vector;

class TestThread
{
public:
	void operator()()
	{
		cout << "TestThread " << std::this_thread::get_id() << endl;
	}
};
int main()
{
	vector<thread> obj;

	for (int i = 0; i < 5; i++)
	{
		obj.push_back(thread(TestThread()));
	}

	cout << "thread still works" << endl;

	std::for_each(obj.begin(), obj.end(), std::mem_fn(&std::thread::join));
	
	cout << "end main()" << endl;

	return 0;
}

/*
run:

TestThread 13816 thread still works
TestThread 14868
TestThread 14536
TestThread 15332

TestThread 6508
end main()

*/

 




answered Feb 6 by avibootz
...