我有一个想法如何制作非常简单的跨平台(linux/windows)线程功能。这是我的示例代码:
#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
恐怕这首先看起来像一个奇怪的问题,但请记住,我是初学者,我需要了解 C++。随意编辑那些我留下“我不知道”评论的部分。
如果您认为这是一个错误的问题,请留下关于我应该如何提问的评论。