#include <windows.h>
DWORD WINAPI ThreadFunc(void* data) {
// Do stuff. This will be the first function called on the new thread.
// When this function returns, the thread goes away. See MSDN for more details.
return 0;
}
int main() {
HANDLE thread = CreateThread(NULL, 0, ThreadFunc, NULL, 0, NULL);
if (thread) {
// Optionally do stuff, such as wait on the thread.
}
}
您还可以选择调用 SHCreateThread()——基本思想相同,但如果你要求的话,它会为你做一些 shell 类型的初始化,比如初始化 COM 等。
Here is the MSDN sample on how to use CreateThread() on Windows.
The basic idea is you call CreateThread() and pass it a pointer to your thread function, which is what will be run on the target thread once it is created.
The simplest code to do it is:
#include <windows.h>
DWORD WINAPI ThreadFunc(void* data) {
// Do stuff. This will be the first function called on the new thread.
// When this function returns, the thread goes away. See MSDN for more details.
return 0;
}
int main() {
HANDLE thread = CreateThread(NULL, 0, ThreadFunc, NULL, 0, NULL);
if (thread) {
// Optionally do stuff, such as wait on the thread.
}
}
You also have the option of calling SHCreateThread()—same basic idea but will do some shell-type initialization for you if you ask it, such as initializing COM, etc.
发布评论
评论(3)
以下是关于如何使用 CreateThread 的 MSDN 示例 () 在 Windows 上。
基本思想是调用 CreateThread() 并向其传递一个指向线程函数的指针,该函数在创建目标线程后将在目标线程上运行。
最简单的代码是:
您还可以选择调用 SHCreateThread()——基本思想相同,但如果你要求的话,它会为你做一些 shell 类型的初始化,比如初始化 COM 等。
Here is the MSDN sample on how to use CreateThread() on Windows.
The basic idea is you call CreateThread() and pass it a pointer to your thread function, which is what will be run on the target thread once it is created.
The simplest code to do it is:
You also have the option of calling SHCreateThread()—same basic idea but will do some shell-type initialization for you if you ask it, such as initializing COM, etc.
您将使用 CreateThread 函数。
您也提到了信号量。为此,您可以使用 CreateSemaphore。
You would use the CreateThread function.
You mentioned semaphores as well. For that you would use CreateSemaphore.
原子操作和互斥体都很好。我使用 CreateThread 等,而不是 pthreads。
Atomic operations and mutexes are good. I use CreateThread etc, not pthreads.