This is a discussion on Closing a thread? within the C Programming forums, part of the General Programming Boards category; x123...
Last edited by Paul22000; 11-13-2008 at 03:03 AM.
Under Posix, multi-threaded access to memory (read and write) must be synchronized. You should use a condition variable to signal the thread to exit.
The more likely cause of your problem however is that the thread is blocked within the accept() call. You'll have to use non-blocking sockets with select() or poll() if you want your while loop to execute at a regular interval.
Nope. What happens if thread A moves the value of your variable into its registers and suddenly it becomes unscheduled. Now B will pick up, change it to something, and A will become scheduled still with the original value of 0 in its registers. Under *nix you should look into a semaphore. Under windows a CriticalSection or Shared Read Write.
>> Or is that something else?
I was referring to pthread_cond_t - a Pthreads synchronization primitive.
>> Is there any way at all to force it to exit?
There's the topic "pthread cancellation", but that's not going to help you. You need to make your code shutdown properly. The first step is to use non-blocking sockets.