Thread: Closing a thread?

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    167

    Closing a thread?

    x123
    Last edited by Paul22000; 11-13-2008 at 03:03 AM.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    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.

    gg

  3. #3
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    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.

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> Or is that something else?
    I was referring to pthread_cond_t - a Pthreads synchronization primitive.
    http://www.opengroup.org/onlinepubs/...cond_init.html

    >> 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.
    http://beej.us/guide/bgnet/output/ht....html#blocking

    gg

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> Is this correct?
    No.

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Terminating secondary thread from another thread
    By wssoh85 in forum C++ Programming
    Replies: 13
    Last Post: 12-19-2008, 05:14 AM
  2. Thread Prog in C language (seg fault)
    By kumars in forum C Programming
    Replies: 22
    Last Post: 10-09-2008, 01:17 PM
  3. Shared memory implementation using thread
    By kumars in forum C Programming
    Replies: 5
    Last Post: 06-18-2008, 04:24 AM
  4. pointer to main thread from worker thread?
    By draegon in forum C++ Programming
    Replies: 2
    Last Post: 10-27-2005, 06:35 AM
  5. Critical Sections, destroying
    By Hunter2 in forum Windows Programming
    Replies: 4
    Last Post: 09-02-2003, 10:36 PM