c++简单线程封装(三)

2013-01-01 14:48:32 · 作者: · 浏览: 1315

 

  [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;

  }