设为首页 加入收藏

TOP

c++简单线程封装(五)
2013-01-01 14:48:32 来源: 作者: 【 】 浏览:1287
Tags:简单 线程 封装

 

  [cpp]

  #pragma once

  template <class T>

  class singleton {

  public:

  static inline T& instance() {

  static T _instance;

  return _instance;

  }

  };

  测试

  [cpp]

  #include <windows.h>

  #include <iostream>

  using namespace std;

  #include "thread.h"

  using namespace thread;

  void S1() {

  while(1) {

  interruption_point();

  cout 《 "S1()" 《 endl;

  Sleep(1000);

  }

  }

  void S2() {

  while(1) {

  interruption_point();

  cout 《 "S2()" 《 endl;

  Sleep(500);

  }

  }

  int main() {

  thread t1(S1);

  thread t2;

  t2 = S2;

  thread t3([&]() {

  Sleep(2000);

  t2.interrupt();

  t2.join();

  Sleep(2000);

  t2.start();

  cout 《 "t3 over" 《 endl;

  });

  t1.start();// always running

  t2.start();// interrupted after 2 seconds and restart after 4 seconds

  t3.start();

  t1.join();

  t2.join();

  return 0;

  }

        

首页 上一页 2 3 4 5 下一页 尾页 5/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇ParticleSystem 粒子系统 下一篇C++11 lambda表达式

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: