首先需要安装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条)