设为首页 加入收藏

TOP

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

 

  DWORD WINAPI ThreadFunc(void* thrd) {

  tls_alloc_once::instance();

  if(current_thread_tls_key == TLS_OUT_OF_INDEXES)

  throw std::exception("tls alloc error");

  if(!::TlsSetValue(current_thread_tls_key, thrd))

  throw std::exception("tls setvalue error");

  try {

  static_cast<thread*>(thrd)->_data.run();

  } catch(interrupt_exception&) {}

  return 0;

  }

  void interruption_point() {

  thread* thrd = get_current_thread_data();

  if(!thrd) throw std::exception("no thread, wth");

  if(thrd->_data.interrupted) {

  thrd->_data.interrupted = false;

  throw interrupt_exception();

  }

  }

  thread* get_current_thread_data() {

  if(current_thread_tls_key == TLS_OUT_OF_INDEXES) {

  return NULL;

  }

  return (thread*)TlsGetValue(current_thread_tls_key);

  }

  }; // end of namespace thread

  locker.h

  [cpp]

  #pragma once

  #include "windows.h"

  class locker {

  public:

  locker() {

  ::InitializeCriticalSection(&cs);

  }

  ~locker() {

  ::DeleteCriticalSection(&cs);

  }

  void lock() const {

  ::EnterCriticalSection(&cs);

  }

  void unlock() const {

  ::LeaveCriticalSection(&cs);

  }

  private:

  mutable ::CRITICAL_SECTION cs;

  };

  singleton.h

        

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

评论

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