【发布时间】:2017-05-05 05:29:44
【问题描述】:
这是我第一次处理线程。
当我在没有GetCurrentThreadId() 函数的情况下运行程序时,它的执行没有任何问题。
当我添加那行代码时,它仍然会执行,但一旦到达末尾就会崩溃。这是为什么呢?
#include <Windows.h>
#include <stdio.h>
#include <conio.h>
static int tix[500];
static int done = 0;
HANDLE ghSemaphore;
DWORD WINAPI ThreadFunction();
int main(void)
{
DWORD threadID1, threadID2, threadID3, threadID4;
HANDLE hThread1, hThread2, hThread3, hThread4;
for (int i = 0; i < 500; i++) //initialize array
{
tix[i] = 0;
}
ghSemaphore = CreateSemaphore(NULL, 1, 10, NULL);
hThread1 = CreateThread(NULL, 0, ThreadFunction, NULL, 0, &threadID1);
hThread2 = CreateThread(NULL, 0, ThreadFunction, NULL, 0, &threadID2);
hThread3 = CreateThread(NULL, 0, ThreadFunction, NULL, 0, &threadID3);
hThread4 = CreateThread(NULL, 0, ThreadFunction, NULL, 0, &threadID4);
//printf("The thread ID: %d.\n", threadID1);
//printf("The thread ID: %d.\n", threadID2);
//printf("The thread ID: %d.\n", threadID3);
//printf("The thread ID: %d.\n", threadID4);
if (done = 1)
{
CloseHandle(hThread1);
CloseHandle(hThread2);
CloseHandle(hThread3);
CloseHandle(hThread4);
}
for (int j = 0; j < 500; j++)
{
if (tix[j] = 0)
{
printf("not sold");
}
else if (tix[j] = 1)
{
printf("sold");
}
}
return 0;
}
DWORD WINAPI ThreadFunction()
{
WaitForSingleObject(ghSemaphore, 0);
printf("current thread running : %d\n", GetCurrentThreadId());
int i = 0;
if (done != 0) // if loop to test wether or not the array is full
{
while (tix[i] = 1) //traverse the array to find a open spot
{
i++;
}
tix[i] = 1;
}
if (i == 499) //if i is 499, set test variable to 1
{
done = 1;
return 0;
}
ReleaseSemaphore(ghSemaphore, 1, NULL);
}
【问题讨论】:
-
崩溃时的错误信息是什么?
-
您的线程函数的签名不正确,因此您可能正在破坏堆栈。不要忽略编译器警告。此外,
if (done = 1)和if (tix[j] = 1)将始终为真。我建议你在尝试多线程之前掌握基本的 C。 -
您的代码永远不会关闭线程句柄。在函数结束时,您需要 WaitForMultipleObjects 并确保在关闭句柄之前所有线程都已执行完毕。不用说,变量
done在多线程程序中是无稽之谈。 -
@Lundin:您确实不必必须等待线程运行完成才能关闭其句柄。如果您的代码不需要它,它可以在
CreateThread返回后立即关闭句柄。 -
@IInspectable 然而,在清理和关闭主进程之前等待所有线程完成是一个好习惯。
标签: c multithreading winapi