一個(gè)簡單的多線程例子
#include <iostream>
#include <thread>
void foo(int i)
{
std::thread::id this_id = std::this_thread::get_id();
std::cout << "thread_func: id = " << this_id << ", " << i << std::endl;
}
int main()
{
for (uint8_t i = 0; i < 4; i++)
{
std::thread t(foo, i);
t.detach();
}
getchar();
return 0;
}
這個(gè)程序里面創(chuàng)建了4個(gè)線程,每次創(chuàng)建的時(shí)候分別傳遞一個(gè)標(biāo)簽進(jìn)去冷溃,運(yùn)行一次钱磅,我們可能會(huì)看到如下的輸出,
thread_func: id = 139708503934720, 3
thread_func: id = 139708512327424, 2
thread_func: id = 139708520720128, 1
thread_func: id = 139708529112832, 0
可以看到這些線程的調(diào)度運(yùn)行的順序并不一定按照創(chuàng)建線程的先后似枕,它有可能是任意的盖淡;
多運(yùn)行幾次后,我們可能會(huì)看到如下的打印
thread_func: id = thread_func: id = 140703835129600, 2
140703851915008, 0
thread_func: id = 140703843522304, 1
thread_func: id = 140703826736896, 3
顯然的凿歼,第0個(gè)線程在打印的時(shí)候被第2個(gè)線程中斷了褪迟,所以打印出來是這樣。
那怎么解決這個(gè)問題呢答憔,我們就引入了線程鎖
引入線程鎖
修改后的程序如下
#include <iostream>
#include <thread>
#include <mutex>
std::mutex g_display_mutex;
void foo(int i)
{
std::thread::id this_id = std::this_thread::get_id();
g_display_mutex.lock();
std::cout << "thread_func: id = " << this_id << ", " << i << std::endl;
g_display_mutex.unlock();
}
int main()
{
for (uint8_t i = 0; i < 4; i++)
{
std::thread t(foo, i);
t.detach();
}
getchar();
return 0;
}
這樣打印消息的時(shí)候就再也不會(huì)出現(xiàn)中斷的情況了味赃。
detach和join
C++有兩種方式來等待線程結(jié)束
- detach在這里表示啟動(dòng)的線程自己在后臺(tái)運(yùn)行,當(dāng)前的代碼繼續(xù)運(yùn)行虐拓,不等待新的線程結(jié)束
- join的方式實(shí)在線程運(yùn)行完成后再繼續(xù)往下運(yùn)行心俗,所以如果對之前的代碼再做修改如下
int main()
{
for (int i = 0; i < 4; i++)
{
std::thread t(foo, i);
t.join();
std::cout << "main: " << i << std::endl;
}
getchar();
return 0;
}
這個(gè)時(shí)候每次main函數(shù)打印的時(shí)候都會(huì)等待新創(chuàng)建的線程運(yùn)行完畢,所以固定的會(huì)答應(yīng)如下
thread_func: id = 139945708939008, 0
main: 0
thread_func: id = 139945708939008, 1
main: 1
thread_func: id = 139945708939008, 2
main: 2
thread_func: id = 139945708939008, 3
main: 3
關(guān)于線程的異常終結(jié)
假如說以detach的方式蓉驹,讓線程在后臺(tái)運(yùn)行城榛,那么線程的實(shí)例和線程本身就分離開了;有可能出現(xiàn)一種場景态兴,
- 主函數(shù)退出了狠持,所以維護(hù)的線程實(shí)例也會(huì)自然銷毀
- 但是這個(gè)時(shí)候線程并沒有運(yùn)行完成,會(huì)出現(xiàn)異常退出的場景
處理這種問題的方法可以有兩種
- 在線程實(shí)例有可能會(huì)被銷毀前瞻润,調(diào)用一下join喘垂,等待線程完結(jié)甜刻;
- 或者通過類的析構(gòu)函數(shù),創(chuàng)建類的實(shí)例的時(shí)候創(chuàng)建線程實(shí)例正勒,等到銷毀這個(gè)類的時(shí)候會(huì)自動(dòng)調(diào)用析構(gòu)函數(shù)得院,在析構(gòu)函數(shù)里面等待線程運(yùn)行完結(jié);
class foo
{
public:
foo();
~foo();
private:
std::thread t;
};
foo::foo()
{
t = std::thread([] {
std::thread::id this_id = std::this_thread::get_id();
std::cout << "thread: sleep for 3s! " << std::endl;
sleep(3);
g_display_mutex.lock();
std::cout << "thread id = " << this_id << std::endl;
g_display_mutex.unlock();
});
}
foo::~foo()
{
std::cout << "destructor called" << std::endl;
if (t.joinable())
{
std::cout << "joinable" << std::endl;
t.join();
}
}
int main()
{
foo m_foo;
return 0;
}
運(yùn)行打印的結(jié)果如下昭齐,
destructor called
joinable
thread: sleep for 3s!
thread id = 139909607855872
- main函數(shù)創(chuàng)建完類的實(shí)例m_foo后就會(huì)退出函數(shù)
- main函數(shù)退出的同時(shí)需要銷毀m_foo尿招,會(huì)自動(dòng)調(diào)用析構(gòu)函數(shù)矾柜;
- 析構(gòu)函數(shù)會(huì)等待線程t的運(yùn)行完成然后再完成類foo的析構(gòu)阱驾;