📅  最后修改于: 2020-12-15 04:27:28             🧑  作者: Mango
线程是指令序列,可以在多线程环境中与其他此类序列同时执行,同时共享相同的地址空间。
Sr.No. | Member type & description |
---|---|
1 | id
It is a thread id. |
2 | Native handle type
It is a native handle type. |
Sr.No. | Member function & description |
---|---|
1 | (constructor)
It is used to construct thread. |
2 | (destructor)
It is used to destructor thread. |
3 | operator=
It is a move-assign thread. |
4 | get_id
It is used to get thread id. |
5 | joinable
It is used to check if joinable. |
6 | join
It is used to join thread. |
7 | detach
It is used to detach thread. |
8 | swap
It is used to swap threads. |
9 | native_handle
It is used to get native handle. |
10 | hardware_concurrency [static]
It is used to detect hardware concurrency. |
Sr.No. | Non-member overload & description |
---|---|
1 | swap (thread)
It is used to swap threads. |
在下面的std :: thread示例中。
#include
#include
void foo() {
std::cout << " foo is executing concurrently...\n";
}
void bar(int x) {
std::cout << " bar is executing concurrently...\n";
}
int main() {
std::thread first (foo);
std::thread second (bar,0);
std::cout << "main, foo and bar now execute concurrently...\n";
first.join();
second.join();
std::cout << "foo and bar completed.\n";
return 0;
}
输出应该是这样的-
main, foo and bar now execute concurrently...
bar is executing concurrently...
foo is executing concurrently...
foo and bar completed.