c++ 11 thread 初試,11thread初試

來源:互聯網
上載者:User

c++ 11 thread 初試,11thread初試

最新的 c++11標準整合進了 線程支援,下面寫一個小程式測試一下。

測試代碼:

#include <iostream>#include <thread>void hello(void){std::cout << "Hello concurrent world" << std::endl;}int main(void){std::thread t(hello);t.join();}

編譯方法:

g++ -std=c++11  thread1.cpp   -pthread  -g -o thread

可見,還是需要 pthreads庫支援。。

在 hello()加個斷點,看一下此時的調用棧:

#0  hello () at thread1.cpp:6#1  0x08049b27 in std::_Bind_simple<void (*())()>::_M_invoke<>(std::_Index_tuple<>) (this=0x804e024) at /usr/include/c++/4.8/functional:1732#2  0x08049aad in std::_Bind_simple<void (*())()>::operator()() (    this=0x804e024) at /usr/include/c++/4.8/functional:1720#3  0x08049a62 in std::thread::_Impl<std::_Bind_simple<void (*())()> >::_M_run() (this=0x804e018) at /usr/include/c++/4.8/thread:115#4  0xb7f76d1e in ?? () from /usr/lib/i386-linux-gnu/libstdc++.so.6#5  0xb7e9ff70 in start_thread (arg=0xb7ca1b40) at pthread_create.c:312#6  0xb7dd6bee in clone () at ../sysdeps/unix/sysv/linux/i386/clone.S:129

可以看到, gcc 4.8裡面, c++11的線程基本上是對 pthreads的封裝。

我使用的 gcc版本是 4.8.2


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.