C 未使用ResetEvent()重置事件
而(1)在事件循环中继续,即使事件是重新设置的。下面是我的代码。我在代码中的注释中提到了问题所在的实际问题。提前感谢您的帮助:)C 未使用ResetEvent()重置事件,c,multithreading,events,synchronization,critical-section,C,Multithreading,Events,Synchronization,Critical Section,而(1)在事件循环中继续,即使事件是重新设置的。下面是我的代码。我在代码中的注释中提到了问题所在的实际问题。提前感谢您的帮助:) 必须使用参数bManualReset TRUE创建事件才能使ResetEvent()正常工作。实际上,您忘记了if(sendBuff==NULL)检查中的LeaveCriticalSection调用。这可能会阻塞您的Writer线程 DWORD WINAPI workerThreadProcedure(LPVOID lparam) { prin
必须使用参数bManualReset TRUE创建事件才能使ResetEvent()正常工作。实际上,您忘记了if(sendBuff==NULL)检查中的LeaveCriticalSection调用。这可能会阻塞您的Writer线程
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;
}