C++11中已經擁有了一個更好用的用于線程操作的類`std::thread`。
默認構造函數:?
thread() noexcept;?
構造一個任何線程不執行的線程對象。
初始化函數:
~~~
template <class Fn, class... Args>
explicit thread (Fn&& fn, Args&&... args);
~~~
構造一個線程對象,可以開啟線程執行?
新執行的線程調用函數fn,并傳遞args作為參數?
fn?
可以指向函數,指向成員,或是移動構造函數?
args…?
傳遞給fn的參數,這些參數可以移動賦值構造。如果fn是一個成員指針,那么第一個args參數就必須是一個對象,或是引用,或是指向該對象的指針。
拷貝構造函數:?
thread (const thread&) = delete;?
刪除構造的線程
現在介紹幾個成員函數:?
**std::thread::join**?
該函數返回時,線程執行完成。?
當 a thread 調用Join方法的時候,MainThread 就被停止執行,直到 a thread 線程執行完畢。
~~~
#include <iostream> // std::cout
#include <thread> // std::thread, std::this_thread::sleep_for
#include <chrono> // std::chrono::seconds
void pause_thread(int n)
{
std::this_thread::sleep_for (std::chrono::seconds(n));
std::cout << "pause of " << n << " seconds ended\n";
}
int main()
{
std::cout << "Spawning 3 threads...\n";
std::thread t1 (pause_thread,1);
std::thread t2 (pause_thread,2);
std::thread t3 (pause_thread,3);
std::cout << "Done spawning threads. Now waiting for them to join:\n";
t1.join();
t2.join();
t3.join();
std::cout << "All threads joined!\n";
return 0;
}
//輸出
Output (after 3 seconds):
Spawning 3 threads...
Done spawning threads. Now waiting for them to join:
pause of 1 seconds ended
pause of 2 seconds ended
pause of 3 seconds ended
All threads joined!
~~~
**std::thread::detach**?
分離線程的對象,使它們從彼此獨立地執行所表示的線程。這兩個線程繼續沒有阻止,也沒有以任何方式同步。注意,當任一結束執行,其資源被釋放。
~~~
#include <iostream> // std::cout
#include <thread> // std::thread, std::this_thread::sleep_for
#include <chrono> // std::chrono::seconds
void pause_thread(int n)
{
std::this_thread::sleep_for (std::chrono::seconds(n));
std::cout << "pause of " << n << " seconds ended\n";
}
int main()
{
std::cout << "Spawning and detaching 3 threads...\n";
std::thread (pause_thread,1).detach();
std::thread (pause_thread,2).detach();
std::thread (pause_thread,3).detach();
std::cout << "Done spawning threads.\n";
std::cout << "(the main thread will now pause for 5 seconds)\n";
// give the detached threads time to finish (but not guaranteed!):
pause_thread(5);
return 0;
}
//輸出
Output (after 5 seconds):
Spawning and detaching 3 threads...
Done spawning threads.
(the main thread will now pause for 5 seconds)
pause of 1 seconds ended
pause of 2 seconds ended
pause of 3 seconds ended
pause of 5 seconds ended
~~~
上一段代碼 深入理解:
~~~
#include <iostream>
#include <thread>
using namespace std;
class Foo
{
void bar_i() { cout << "hello" << endl; }
public:
void bar()
{
auto func = std::bind(&Foo::bar_i, this);
std::thread t(&Foo::bar_i, std::ref(*this));
t.join();
}
};
int main()
{
Foo f;
f.bar();
}
~~~
如果你使用的是VS2015,那么恭喜你,上面的代碼你不會運行成功。
- 前言
- 吐血整理C++11新特性
- C++11新特性之std::function
- c++11特性之正則表達式
- c++11特性之Lambda表達式
- c++11特性之override和final關鍵字
- c++11特性之std::thread--初識
- c++11特性之std::thread--初識二
- c++11特性之initializer_list
- c++11特性之std::thread--進階
- c++11特性之std::thread--進階二
- C++11新特性之 CALLBACKS
- C++11新特性之 std::array container
- C++11新特性之 nullptr
- C++11新特性之 rvalue Reference(右值引用)
- C++11新特性之 Move semantics(移動語義)
- C++11新特性之 default and delete specifiers
- C++11新特性之 Static assertions 和constructor delegation
- 開始使用C++11的幾個理由
- C++11新特性之 std::future and std::async