Re: Problem interrupting threads in PR_Accept() wiht PR_Interrupt()
"Wan-Teh Chang" <[email protected]>
| Newsgroups | gmane.comp.mozilla.devel.nspr |
|---|---|
| Message-ID | <[email protected]> |
On Wed, Aug 6, 2008 at 7:38 AM, Joachim Ziegler <[email protected]> wrote: > Hello, > > to get familiarized with NSPR, I have written a little demo web server. > > The server has master thread that creates 5 worker threads. Each worker > thread blocks in PR_Accept() to wait for a new request, then serves the > request, the goes on in an endless loop. This loop looks like this: > > > while( 1 ) { > if((fd = PR_Accept( sock, NULL, PR_INTERVAL_NO_TIMEOUT )) > == NULL) { > if(PR_GetError() == PR_PENDING_INTERRUPT_ERROR) > break; /* that's OK: master has signaled an interrupt */ > else { > oops("PR_Accept in thread_start()"); > continue; > } > } > handle_call( fd ); > PR_Close( fd ); > } I found a problem in your code, which should explain what you observed. I/O functions that may block indefinitely, for example, PR_Recv and PR_Send, can also be interrupted. Your handle_call function must be calling such functions. So you also need to check for the PR_PENDING_INTERRUPT_ERROR error in handle_call, and exit the worker thread's while loop if you get that error. You can also just use a global variable: PRBool stopping = PR_FALSE; Change your worker thread's while loop to test it: while(!stopping) And your master thread sets 'stopping' to PR_TRUE before interrupting the worker threads: stopping = PR_TRUE; for(nthread = 0; nthread < NTHREADS; nthread++) if( PR_Interrupt(worker[nthread]) == PR_SUCCESS ) printf("MASTER[%d]: Thread %d is blocked. Sent PR_Interrupt.\n", myPID, nthread); else printf("MASTER[%d]: Thread %d is not blocked. Sent PR_Interrupt\n", myPID, nthread); Wan-Teh