Thread: Close Thread which manage sockets

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    14

    Question Close Thread which manage sockets

    Hello!

    I've a multiwindows multithreaded program.
    Into a thread, I open socket to receive data with infinite loop. This thread was created by : MyThread = CreateThread(NULL, 0, MySocketsFunction, &MyDatas, 0, NULL);

    When user wants stop listening, he clicks on STOP button.
    Which makes a : TerminateThread(MyThread,0);


    The problem is my thread was listening (recvfrom) on port when it was stopped by TerminateThread.
    But as function is suddently stopped, socket is not closed with closesocket(IdMonSocket); because this calling is at the end of the function

    So, I can see with netstat -a, that my port is already opened, but thread was terminated.

    How can I make to close socket when I terminate thread ?

    Thank You!

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    There are lots of sane ways to close a thread... TerminateThread() isn't one of them.

    If it has no message handler (i.e. is not tossing messages) the easiest way is to set a global variable ... ThreadStop or such... and use that as the condition in a while() statement... When the stop condition is met you exit your loop and let the thread function return normally.
    Code:
    // at global scope
    BOOL ThreadStop = FALSE;
    
    // inside the threadproc
    while (!ThreadStop)
      {
          // do winsock stuff
       }
    // here you close your ports
    ExitThread(0);
    }
    
    
    // user clicks stop button (in main message loop)
    case id_Stop :
       ThreadStop = 1;
    Of course if the thread is tossing window messages you can just post a message to the message proc that tells it to exit.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    14
    Hello!

    Thanks for your answer !
    My thread doesn't use tossing window messages.

    I try to use global variable at least ! But as I can read, it seems to be the only solution???

    How use tossing window messages with threads ?

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> the easiest way is to set a global variable ... ThreadStop or such
    No, that isn't one of them either. When two or more threads access the same memory location, and at least one is a write access, then you must use synchronization primitives provided by the threading library/compiler.

    Here is an example of using a Win32 event object to signal a thread to exit - at which point the thread can perform cleanup before exiting.
    Odd Sleep() Within Thread Behavior

    http://cboard.cprogramming.com/windows-programming/134597-odd-sleep-within-thread-behavior.html#post1002234

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [w32 Api] Get Exit Code from Thread without blocking program
    By theclem35 in forum Windows Programming
    Replies: 9
    Last Post: 04-07-2011, 04:06 PM
  2. [code] Win32 Thread Object
    By Codeplug in forum Windows Programming
    Replies: 0
    Last Post: 06-03-2005, 03:55 PM
  3. multithreading question
    By ichijoji in forum C++ Programming
    Replies: 7
    Last Post: 04-12-2005, 10:59 PM
  4. Replies: 12
    Last Post: 05-17-2003, 05:58 AM
  5. Ghost in the CD Drive
    By Natase in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 10-12-2001, 05:38 PM