首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >没有特定成员名称的c++ CreateThread非静态成员

没有特定成员名称的c++ CreateThread非静态成员
EN

Stack Overflow用户
提问于 2018-05-27 23:31:25
回答 3查看 363关注 0票数 0

我正在尝试创建一个包含非静态类成员的线程,如下所示:

代码语言:javascript
复制
template <class clName>
 DWORD WINAPI StartThread(PVOID ptr) {
   ((clName*)(ptr))->testf(); // this is static member name I want to be able use different names with the same function
   return 1;
 }

class Thread {
  private :
  HANDLE native_handle = 0;
  DWORD id = 0;
public :
  template <class T,class U>
  Thread(T U::*member,U* original); // I want to use different members with the same function
  bool run();
}

template<class T,class U>
Thread::Thread(T U::*member, U* original)
{
  native_handle = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)StartThread<U>,original, CREATE_SUSPENDED, &id);
}

bool Thread::run() {
  DWORD res = ResumeThread(native_handle);
  if (res == (DWORD)-1) return false;
  return true;
}


class testt {
public :
  void testf() {
    MessageBoxA(0, "working", "", 0);
  }
  void doIt() {
    Thread t(&testt::testf,this);
    t.run();
  }
};

int main() {
  testt tt;
  tt.doIt();
}

如你所见,我只能运行一个特定的成员,所以这个方法是不可移植的,不能用于任何类或成员。

我知道我可以很容易地使用std::thread,但我正在处理一个不应该使用任何C++运行时的项目,所以我为new/delete、线程、文件I/O等创建包装器。否则,我总是使用std::thread,它非常棒。在这个项目中,我只能使用Win32应用编程接口。

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50553714

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档