下面是网上的解释:
Thread
Class to represent individual threads of execution.
A thread of execution is a sequence of instructions that can be executed concurrently with other such sequences in multithreading environments, while sharing a same address space.
An initialized thread object represents an active thread of execution; Such a thread object is joinable, and has a unique thread id.
A default-constructed (non-initialized) thread object is not joinable, and its thread id is common for all non-joinable threads.
A joinable thread becomes not joinable if moved from, or if either join or detach are called on them.
- Member types
- id:Thread id (public member type )
- native_handle_type: Native handle type (public member type )
- Member functions
- (constructor):Construct thread (public member function )
- (destructor):Thread destructor (public member function )
- operator= :Move-assign thread (public member function
- get_id:Get thread id (public member function )
- joinable :Check if joinable (public member function )
- join :Join thread (public member function )
- detach:Detach thread (public member function )
- swap:Swap threads (public member function )
- native_handle:Get native handle (public member function )
- hardware_concurrency [static]:Detect hardware concurrency (public static member function )
- Non-member overloads
- swap (thread):Swap threads (function )
举例说明:
// thread example
#include <iostream> // std::cout
#include <stdio.h>
#include <thread> // std::thread
void foo()
{
std::thread::id firstID = std::this_thread::get_id();
printf("first thread id is %ld\n", firstID);
std::cout<<"first thread id is "<<firstID << std::endl;
}
void bar(int x)
{
std::thread::id secondID = std::this_thread::get_id();
printf("second thread id is %ld\n", secondID);
std::cout<<"second thread id is "<<secondID << std::endl;
}
int main()
{
std::thread first (foo); // spawn new thread that calls foo()
std::thread second (bar,0); // spawn new thread that calls bar(0)
std::cout << "main, foo and bar now execute concurrently...\n";
// synchronize threads:
first.join(); // pauses until first finishes
second.join(); // pauses until second finishes
std::cout << "foo and bar completed.\n";
return 0;
}
g++ main.cpp -std=c++11 -lpthread
main, foo and bar now execute concurrently...
second thread id is 140619849778944
second thread id is 140619849778944
first thread id is 140619858171648
first thread id is 140619858171648
foo and bar completed.
可以看出,对于x64而言,thread ID是long int 型。