How to get thread id in C++

1 Answer

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

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

void thread_function()
{
	cout << "thread_function id: " << std::this_thread::get_id() << endl;
}
int main()
{
	thread obj1(thread_function);
	thread obj2(thread_function);

	cout << "main() thread obj1 id: " << obj1.get_id() << endl;
	cout << "main() thread obj2 id: " << obj2.get_id() << endl;

	obj1.join();
	obj2.join();

	return 0;
}

/*
run:

thread_function id: 8948
main() thread obj1 id: 8948
main() thread obj2 id: 5320
thread_function id: 5320

*/

 



answered Feb 6, 2018 by avibootz

Related questions

1 answer 253 views
253 views asked May 18, 2018 by avibootz
1 answer 182 views
182 views asked Jan 29, 2024 by avibootz
1 answer 281 views
1 answer 224 views
224 views asked May 18, 2018 by avibootz
1 answer 129 views
129 views asked Sep 1, 2024 by avibootz
2 answers 256 views
256 views asked Feb 7, 2018 by avibootz
...