boost库之thread练习

     线程在进行多个任务同时运行的时候,其消耗是相当小的。因此在诸多的多任务应用用起到了很关键的作用,尤其是几十,上百任务时就会有所体现,单单一两个不足以体现线程使用的优势。因此在开发多任务程序时掌握多线程的设计也至关重要。今天小试了下boost库中的thread,使用上和C下的thread没多大差别,毕竟是应用而不是写库,拿来看到即可用上。

点击(此处)折叠或打开

  1. #include <iostream>
  2. #include <cassert>
  3. #include <string>
  4. #include <iomanip>
  5. #include <boost/thread/thread.hpp>
  6. #include "boost/regex.hpp"
  7. using namespace std;
  8. void print_hello(void){
  9.     while(1){
  10.         std::cout<<"hello\n"<<std::endl;
  11.         std::cout<<pthread_self()<<std::endl;
  12.         sleep(1);
  13.     }
  14. }

  15. void print_nice(void){
  16.     while(1){
  17.         std::cout<<"nice\n"<<std::endl;
  18.         std::cout<<pthread_self()<<std::endl;
  19.         usleep(500000);
  20.     }
  21. }

  22. int main(int argc,char *argv[]) {
  23.     int i;
  24.     i=10;
  25.     std::cout<<std::endl;
  26.     boost::thread thread1(&print_hello);
  27.     boost::thread thread2(&print_nice);
  28.     cout<<dec<<i<<" "<<hex<<i<<endl;
  29.     cout<<dec<<thread1.get_id()<<std::endl;
  30.     cout<<dec<<thread2.get_id()<<std::endl;
  31.     thread1.join();
  32.     thread2.join();

  33.     return true;
  34. }
<script type=text/javascript charset=utf-8 src="http://static.bshare.cn/b/buttonLite.js#style=-1&uuid=&pophcol=3&lang=zh"></script> <script type=text/javascript charset=utf-8 src="http://static.bshare.cn/b/bshareC0.js"></script>
阅读(156) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~
评论热议
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值