經(jīng)典用例
#include <iostream>
#include <thread>
void hello()
{
std::cout << "hello concurrent world" << std::endl;
}
int main()
{
std::thread t(hello);
t.join();
}
編譯文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-664954.html
g++ -g test.cpp -o out -lpthread
gdb調(diào)試文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-664954.html
(gdb) r
Starting program: /usr1/code/ch1/out
Missing separate debuginfos, use: zypper install glibc-debuginfo-2.22-100.15.4.x86_64
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".
[New Thread 0x7ffff6f79700 (LWP 17902)]
hello concurrent world
[Thread 0x7ffff6f79700 (LWP 17902) exited]
[Inferior 1 (process 17898) exited normally]
Missing separate debuginfos, use: zypper install libgcc_s1-debuginfo-8.2.1+r264010-1.3.3.x86_64 libstdc++6-debuginfo-8.2.1+r264010-1.3.3.x86_64
(gdb) c
The program is not being run.
(gdb) info threads
No threads.
(gdb) b dm_01_01.cpp:11
Breakpoint 1 at 0x400d53: file dm_01_01.cpp, line 11.
(gdb) r
Starting program: /usr1/code/ch1/out
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".
Breakpoint 1, main () at dm_01_01.cpp:11
11 std::thread t(hello);
(gdb) n
[New Thread 0x7ffff6f79700 (LWP 18588)]
hello concurrent world
[Thread 0x7ffff6f79700 (LWP 18588) exited]
12 t.join();
(gdb) info threads
Id Target Id Frame
* 1 Thread 0x7ffff7fda740 (LWP 18504) "out" main () at dm_01_01.cpp:12
(gdb) d
Delete all breakpoints? (y or n) y
(gdb) b dm_01_01.cpp:6
Breakpoint 2 at 0x400d2b: file dm_01_01.cpp, line 6.
(gdb) c
Continuing.
[Inferior 1 (process 18504) exited normally]
(gdb) r
Starting program: /usr1/code/ch1/out
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".
[New Thread 0x7ffff6f79700 (LWP 18896)]
[Switching to Thread 0x7ffff6f79700 (LWP 18896)]
Thread 2 "out" hit Breakpoint 2, hello () at dm_01_01.cpp:6
6 std::cout << "hello concurrent world" << std::endl;
(gdb) info threads
Id Target Id Frame
1 Thread 0x7ffff7fda740 (LWP 18895) "out" 0x00007ffff7bc7a0d in pthread_join ()
from /lib64/libpthread.so.0
* 2 Thread 0x7ffff6f79700 (LWP 18896) "out" hello () at dm_01_01.cpp:6
(gdb) c
Continuing.
hello concurrent world
[Thread 0x7ffff6f79700 (LWP 18896) exited]
[Inferior 1 (process 18895) exited normally]
(gdb) q
代碼中注意點(diǎn)
- 管理線程的函數(shù)和類在
<thread>
中聲明,而保護(hù)共享數(shù)據(jù)的函數(shù)和類在其他
頭文件中聲明 - 每個(gè)線程都必須具有一個(gè)初始函數(shù)(initial function),新線程的執(zhí)行從這個(gè)函數(shù)開(kāi)始。對(duì)于應(yīng)用程序來(lái)說(shuō),初始線程是main(),但是對(duì)于其他線程,可以在 std::thread 對(duì)象的構(gòu)造函數(shù)中指定——本例中,被命名為 t 的 std::thread 對(duì)象使用新函數(shù) hello()作為其初始函數(shù)。
- 與直接寫入標(biāo)準(zhǔn)輸出或是從 main()調(diào)用 hello()不同,該程序啟動(dòng)了一個(gè)新的線程來(lái)實(shí)現(xiàn),使線程數(shù)量增加到兩個(gè)——初始線程始于 main(),而新線程始于hello()
- 新的線程啟動(dòng)后,初始線程繼續(xù)執(zhí)行。如果它不等待新線程結(jié)束,它就將繼續(xù)運(yùn)行到main()的結(jié)尾,從而結(jié)束程序——這有可能發(fā)生在新線程運(yùn)行之前。這就是為什么在這里調(diào)用 join()的原因,這會(huì)使得調(diào)用線程(在 main()中)等待與 std::thread
對(duì)象相關(guān)聯(lián)的線程,即這個(gè)例子中的 t。
到了這里,關(guān)于C++并發(fā)編程學(xué)習(xí)01——hello concurrent world的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!