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 250 views
250 views asked May 18, 2018 by avibootz
1 answer 175 views
175 views asked Jan 29, 2024 by avibootz
1 answer 277 views
1 answer 220 views
220 views asked May 18, 2018 by avibootz
1 answer 123 views
123 views asked Sep 1, 2024 by avibootz
2 answers 252 views
252 views asked Feb 7, 2018 by avibootz
...