How to create a thread using function pointer in C++

1 Answer

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

using std::cout;
using std::endl;

void thread_function()
{
	for (int i = 0; i < 100000; i++);

	cout << "thread_function()" << endl;
}

int main()
{

	std::thread threadObj(thread_function);

	for (int i = 0; i < 100000; i++);

	cout << "main()" << endl;

	threadObj.join();

	cout << "end main()" << endl;

	return 0;
}

/*
run:

thread_function()
main()
end main()

*/

 



answered Feb 5, 2018 by avibootz

Related questions

1 answer 197 views
1 answer 183 views
1 answer 214 views
1 answer 110 views
1 answer 123 views
...