How to create a thread using using lambda functions in C++

1 Answer

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

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

int main()
{
	std::thread obj([] {
		for (int i = 0; i < 5; i++)
			cout << "Thread" << endl;
	});

	for (int i = 0; i < 3; i++)
		cout << "main()" << endl;

	obj.join();
	cout << "end main()" << endl;

	return 0;
}

/*
run:

Threadmain()

Thread
main()
Thread
main()
Thread
Thread
end main()

*/

 



answered Feb 6, 2018 by avibootz

Related questions

1 answer 140 views
1 answer 135 views
1 answer 166 views
1 answer 128 views
1 answer 95 views
95 views asked May 15, 2021 by avibootz
1 answer 117 views
...