gpt4 book ai didi

c - 事件未使用 ResetEvent() 重置

转载 作者:太空宇宙 更新时间:2023-11-04 08:56:13 24 4
gpt4 key购买 nike

while(1) 在事件循环中继续,即使事件被重置。下面是我的代码。我已经在问题所在的代码中的注释中提到了实际问题。提前感谢您的帮助:)

 DWORD WINAPI workerThreadProcedure(LPVOID lparam)
{
printf("===Worker Thread===\n");
// Initialize the critical section one time only.
if (!InitializeCriticalSectionAndSpinCount(&cs,
0x00000400) )
return 0;
int num = LOWORD (lparam);
struct node* cur;
struct node* disp = NULL;
EnterCriticalSection(&cs);
printf("Worker Thread in critical section\n");cur =cread();
cur->rollno = num;
cur->n=NULL;
push(cur);
SetEvent(data_available_event); // signal sender thread that data is available
LeaveCriticalSection(&cs);
return 0;
}
DWORD WINAPI senderThreadProcedure(LPVOID lparam)
{
printf("===Sender Thread===\n");
while(1)
{
printf("Inside While\n");
WaitForSingleObject(data_available_event,0);
EnterCriticalSection(&cs);
printf("Sender Thread in critical section\n");
sendBuff = pop();
if(sendBuff == NULL)
{
ResetEvent(data_available_event);
printf("Data not available anymore");
continue;
/*Here what I want is if sendBuff is NULL,
it resets the event and again go back to while(1) and again waits for
data_available_event. But what actually happens it when sendBuff becomes NUll, it
prints "Data not available anymore", goes to while(1) and even there is no
data_avaialble_event, it again enters critical section, again prints "Data not
available anymore" again go to while and again enters critical section and this cycle
runs infinietly :(*/

}
itoa(sendBuff->rollno,sendBuffer,10);
printf("%s\n",sendBuffer);
//printf("%d\n",disp->rollno);
LeaveCriticalSection(&cs);
printf("Sender Thread LEFT critical section\n");

send(AH_glb_socketIdentifier,sendBuffer,strlen(sendBuffer),0);
Sleep(1);
}
return 0;
}

最佳答案

必须使用参数 bManualReset TRUE 创建事件以使 ResetEvent() 工作。

关于c - 事件未使用 ResetEvent() 重置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16870217/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com