c - Can't exitThread with waitForMultipleObjects -
i have 2 threads, first doing:`
while(1){ if(recv(conn_s, (char*) &i, sizeof(i),0)>0){ if(i==28){ setevent(event); //printf("nell if di 28\n"); handle t; sleep(1); //terminatethread(scegli, 0); //if (closehandle(scegli)==0) printf("error close\n"); t=createthread(null, 0, (lpthread_start_routine) avvia_conversazionecl, conn, synchronize, 0); waitforsingleobject(t, infinite);
the second one:
const handle handles[2]={event, stdinhandle}; while(1){ switch(waitformultipleobjects(num, handles, false, 0)){ case 0: exitthread(-1); break; case 1: r=readconsole(stdinhandle, &interlocutore, nlength, &charsread, null); if (r==0){ printf("%d\n", getlasterror()); } //global_value_to_exit=1; int size=strlen(interlocutore); interlocutore[size-2]='\0'; int verifica=verifica_interlocutore(interlocutore); if (verifica==0){ printf("interlocutore non disponibile, riprova: \n"); continue; } int rrr=send(socket, interlocutore, size, 0); if (rrr<=0){ printf("send error\n"); } sleep(1); if(recv(socket, (char*) &ack, sizeof(int), 0)==socket_error){ printf("errore: %d\n", wsagetlasterror()); } printf("ho ricevuto %d\n", (int) ack); if(ack==999){ //sleep(1); handle t; dword tid; t=createthread(null, 0, (lpthread_start_routine) avvia_conversazionecl, conn_s, 0, &tid); waitforsingleobject(t, infinite); closehandle(t); //global_value_to_exit=0; ack=0; vis=createthread(null, 0, (lpthread_start_routine) visualizza_utenti, conn_s, 0, &visid); } //resetevent(stdinhandle); break; case wait_failed: perror("failed: \n"); break; } memset(interlocutore, 0, sizeof(interlocutore)); } return null; }
also:
const handle handles[2]={event, stdinhandle}; event=createevent(null, false, false, null); stdinhandle=getstdhandle(std_input_handle);`
the point is: if write in console things second thread, if 28 receive make second thread exit sett signaled event. @ point waitformultipleobjects should case 0 , exit thread. instead after i've recv 28 , changed status of event, if write in console second thread enters case 1. after writing else seems dead
any advice?
Comments
Post a Comment