我有一个想法如何使非常简单的跨平台(Linux/Windows)的线程功能。这是我的示例代码:克隆C++数据类型为多平台的目的
#if LINUX
#include <pthread.h>
ThreadHandle createThread(???* callback, void* data) { //I dont know what is data type of function pointer, sorry
pthread_t handle;
pthread_create(&handle, 0, callback, (void*)data);
return (ThreadHandle)handle;
}
define_data_type ThreadHandle = pthread_t; //I don't know how this is done at all, sorry
#endif
#if WINDOWS
#include <windows.h>
ThreadHandle createThread(???* callback, void* data) {
HANDLE handle = CreateThread(
NULL, // default security attributes
0, // use default stack size
callback, // thread function name
data, // argument to thread function
0, // use default creation flags
NULL); // returns the thread identifier - I don't need this, do I?
}
define_data_type ThreadHandle = HANDLE; //I don't know how this is done at all, sorry
#endif
恐怕首先会看起来像veird的问题,但继续记住,我初学者,我需要了解C++。随意编辑那些我离开的部分“我不知道”评论。
如果您认为这是错误的问题,请留下评论,我应该如何问。
['std :: thread'](http://en.cppreference.com/w/cpp/thread)是否适用于所有环境? –
哇,那标准::看起来像治愈一切。每次我寻求多平台时,都会有人用std :: function显示出来。非常感谢你。但我仍然对如何做这个跨平台的东西感兴趣。 –
如果你好奇或者你的环境还没有C++ 11,你可以看看Boost,它包含一个线程包装类,它被移植到许多不同的操作系统中。 –