#include <iostream>
int main(int, char **){
std::thread tt([](){ std::cout<<"Thread!"<<std::endl })
tt.join()
} g++ main.cpp -o main.out -pthread -std=c++11//error g++ -Wl,--no-as-needed -std=c++11 -pthread main.cpp -o main.out//ok
这是个很恶心的问题。我乍一看觉得应该是655我再vc6.0上实验(Debug模式)发现结果是555(vc6debug模式汇编代码)004013A8movdwordptr[ebp-4],5;先将5给[ebp-4]004013AFmoveax,dwordptr[ebp-4];再将5传给eax004013B2pusheax将eax压栈004013B3movecx,dwordptr[ebp-4];将5==>ecx004013B6movdwordptr[ebp-20h],ecx将5==>[ebp-20h]004013B9movedx,dwordptr[ebp-20h]将5==>edx004013BCpushedx将5压栈004013BDmoveax,dwordptr[ebp-4]004013C0pusheax;还是将5压栈004013C1pushoffsetstring"%d%d%d\n"(00426000)004013C6movecx,dwordptr[ebp-4]004013C9addecx,1004013CCmovdwordptr[ebp-4],ecx004013CFcallprintf(00401620)004013D4addesp,10h显而易见结果是555了。不过我为了验证又在release下运行了一把,发现结果竟然是656(vc6release模式汇编代码)push6push5push6push407130hcall00401017addesp,10hxoreax,eaxret晕。编译器给优化了。。。显而易见结果是656了还是不死心再看看linux下gcc编译的发现结果也是656(gccdebug)00401326|.C744241C050>movdwordptr[esp+1C],5|现将5放到[esp+1c]中0040132E|.8B44241Cmoveax,dwordptr[esp+1C]|5==>eax00401332|.FF44241Cincdwordptr[esp+1C]|[esp+1c]++[esp+1c]等于6了00401336|.8B54241Cmovedx,dwordptr[esp+1C]|6==>edx0040133A|.8954240Cmovdwordptr[esp+C],edx|这相当于pushedx也就是将6压栈0040133E|.89442408movdwordptr[esp+8],eax|这相当于pusheax也就是将5压栈00401342|.8B44241Cmoveax,dwordptr[esp+1C]|将6放到eax中00401346|.89442404movdwordptr[esp+4],eax|这相当于pusheax也就是将6压栈0040134A|.C70424243040>movdwordptr[esp],00403024|ASCII"%d%d%d",LF00401351|.E836060000call\printf所以结果是656啦gcc的release和debug一样。。还是不死心。有在vc2005的debug下运行了一把发现结果是656??(vc2005debug汇编代码)00411B4Cmovdwordptr[ebp-0Ch],5;5==>[EBP-0c]00411B53moveax,dwordptr[ebp-0Ch]。;5==>eax00411B56movdwordptr[ebp-1194h],eax5==>[ebp-1194h]00411B5Cmovecx,dwordptr[ebp-0Ch]5==>ecx00411B5Faddecx,1ecx++ecx==600411B62movdwordptr[ebp-0Ch],ecx6==>[ebp-0Ch]00411B65movesi,esp00411B67movedx,dwordptr[ebp-0Ch]6==>edx00411B6Apushedx将6压栈00411B6Bmoveax,dwordptr[ebp-1194h];5==>eax00411B71pusheax将5压栈00411B72movecx,dwordptr[ebp-0Ch];6==>ecx00411B75pushecx将6压栈00411B76pushoffsetstring"%d%d%d\n"(41E9ACh)00411B7Bcalldwordptr[__imp__printf(423780h)]00411B81addesp,10h结果是656唉。看来结果就是656啦。。vc2005和以后版本都注意到了这个问题。我也说不上哪个正确,你又相信哪个呢?首先需要安装boost,步骤如下:
下载到 boost_1_49_0.tar.bz2 (当然,其他压缩格式也可以)后,可以把它放在用户目录下,即:~/
解压缩:tar -jxvf boost_1_49_0.tar.bz2
这样,出现文件夹:~/boost_1_49_0
然后进入:$ cd boost_1_49_0
你会发现有一�¸.shå½ä»¤ï¼bootstrap.sh
è¿è¡å®ï¼$ ./bootstrap.sh ï¼boostèªå·±çget startææ¡£ä¸è¯´è®¾ç½®åæ° --prefix=dir å ¶ä¸dirä¸ºä½ æ³æå®çå®è£ æ件夹ï¼æ建议就ä¸ç¨å è¿ä¸ªåæ°ï¼å®ä¼é»è®¤å®è£ å°/usr/localï¼
ç»æååºç°ä¸ä¸ªå¯æ§è¡æä»¶ï¼ ~/boost_1_49_0/b2
è¿è¡è¿ä¸ªæä»¶ï¼ $ sudo ./b2 install ï¼Ubuntuç¨æ·åä¸å«å¿äºå sudoï¼ä¸ç¶å®è£ åå°æ æ³å®å ¨ä½¿ç¨ï¼
ç¼è¯å®è£ æ¶é´æ¯è¾é¿ï¼æ ¹æ®ä¸åæºå¨çæ åµ20ï½40åéãç»æåå³å®è£ å®æ¯ã
boost::threadç使ç¨
#include <boost/thread.hpp>#include <iostream>
void task1() {
// do stuff
std::cout << "This is task1!" << std::endl
}
void task2() {
// do stuff
std::cout << "This is task2!" << std::endl
}
int main (int argc, char ** argv) {
using namespace boost
thread thread_1 = thread(task1)
thread thread_2 = thread(task2)
// do other stuff
thread_2.join()
thread_1.join()
return 0
}
ç¼è¯æ¶çå½ä»¤ä¸ºï¼
$ g++ -I./inlcude -L./lib example.cpp -lboost_thread -o example
ç¼è¯ä¹åä¼åºç°ä¸ä¸ª example çå¯æ§è¡æ件ï¼å¯ä»¥è¿è¡ï¼./example ï¼ ç»ææ¾ç¤ºï¼
This is task2!
This is task1!
å¯è½ä½ å¨è¿è¡æ¶ä¼åºç°è¿æ ·çé误ï¼error while loading shared libraries: libboost_thread.so.1.49.0: cannot open shared object file: No such file or directory
è¿æ¯å 为è¦ç¨å°çåºä¸å¨é»è®¤çç¯å¢åééï¼å¯ä»¥ä½¿ç¨ä¸é¢çå½ä»¤æ·»å ï¼
$ sudo ldconfig /usr/local/lib
æ·»å åï¼åæ§è¡./exampleï¼è¿æ ·ä½ å°±å®æäºä½ ç第ä¸ä¸ªboost::threadç¨åºã
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)