zhtread c++多线程库的使用

很早就买了c++编程思想第二卷这本书 可惜最后一章一直没有看。于是找个时间看看多线程也比较好啊

使用编译好的zthread库

自己先写个简短的例子:
如下:
  1. #include <zthread/runnable.h>
  2. #include <zthread/thread.h>
  3. #pragma comment(lib,"ZThread_Z.lib")
  4. class thread : public ZThread::Runnable
  5. {
  6. public:
  7.     thread(int _id): id(_id){}
  8.     ~thread(){}
  9.     void run()
  10.     {
  11.     printf("i am thread: %d ./n", id);
  12.     }
  13. private:
  14.     int id;
  15. };
  16. int _tmain(int argc, _TCHAR* argv[])
  17. {
  18.       try
  19.       {
  20.            int itr = 0;
  21.            while(itr++<5)
  22.                 ZThread::Thread th(new thread(itr));
  23.            printf("start /n");
  24.       }
  25.       catch(ZThread::Synchronization_Exception& e)
  26.       {
  27.             printf("%s",e.what());
  28.       }
  29.       _sleep(20000);
  30.       return 0;
  31. }
然后再来一段可以同步处理响应的代码段
  1. #include <zthread/runnable.h>
  2. #include <zthread/thread.h>
  3. #include <iostream>
  4. #pragma comment(lib,"ZThread_Z.lib")
  5. class thread : public ZThread::Runnable
  6. {
  7. public:
  8.     thread(int _id): id(_id){}
  9.     ~thread(){}
  10.     void run()
  11.     {
  12.          int i =0 ;
  13.          while(i++<100)
  14.         {
  15.               printf("i am thread: %d ./n", id);
  16.               _sleep(1200);
  17.         }
  18.     }
  19. private:
  20.     int id;
  21. };
  22. int _tmain(int argc, _TCHAR* argv[])
  23. {
  24.       try
  25.       {
  26.            int itr = 0;
  27.            while(itr++<8)
  28.                 ZThread::Thread th(new thread(itr));
  29.            ZThread::Thread(th);
  30.            char ch;
  31.            std::cin>>ch;
  32.            std::cout<<"your input is: "<<ch<<std::endl;
  33.            printf("start /n");
  34.       }
  35.       catch(ZThread::Synchronization_Exception& e)
  36.       {
  37.             printf("%s",e.what());
  38.       }
  39.       _sleep(20000);
  40.       return 0;
  41. }

好好研究研究zthread打算以后用用
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值