Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Prodentim Probiotics Specially Designed For The Health Of Your Teeth And Gums

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Powerful WordPress hosting for WordPress professionals

Disclosure: My content contains affiliate links.

31,166 questions

40,722 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, 2018 by avibootz

Related questions

1 answer 96 views
96 views asked May 18, 2018 by avibootz
1 answer 19 views
1 answer 35 views
...