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

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,990 questions

51,935 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 89 views
1 answer 166 views
166 views asked May 18, 2018 by avibootz
1 answer 107 views
1 answer 158 views
1 answer 159 views
...