我需要什么以及如何在Windows Vista上使用C中的线程?
你能给我一个简单的代码示例吗?
这里是关于如何在Windows上使用CreateThread()的MSDN示例 。
基本的想法是调用CreateThread()并将它传递给你的线程函数的指针,这个线程函数一旦创建就会在目标线程上运行。
最简单的代码是:
#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等。
您将使用CreateThread函数。
你也提到了信号量。 为此,您将使用CreateSemaphore 。
原子操作和互斥是很好的。 我使用CreateThread等,而不是pthreads。