CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jun 2006
    Posts
    46

    AfxSocketInit() unnecessary for worker threads ?

    Hi, internet search reveals that AfxSocketInit() should be called in every thread. I am using CSocket for listening, sending, receiving in 2 worker threads and main thread. The program works most of the time whether I call it or not. Moreover, it is actually more stable without it. Any thoughts ? Thanks.

  2. #2
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: AfxSocketInit() unnecessary for worker threads ?

    You just need one call to AfxSocketInit no matter how many threads you're using. You typically place the call inside of CWinApp::InitInstance.

    - petter

  3. #3
    Join Date
    Jun 2006
    Posts
    46

    Re: AfxSocketInit() unnecessary for worker threads ?

    Thank you for explanation, wildfrog. Various internet articles persuade you to place it in every thread, but I have noticed it causes real havoc, hanging application or even unresponsive windows system.

  4. #4
    Join Date
    Mar 2001
    Posts
    2,529

    Re: AfxSocketInit() unnecessary for worker threads ?

    AfxSocketInit() mostly requests a version of winsock and loads the winsock dll.
    Something like this:
    Code:
    wVersionRequested = MAKEWORD(2, 2);   /* Request Winsock v2.0 */
    if (WSAStartup(wVersionRequested, &wsaData) != 0) /* Load Winsock DLL */
    HTH,
    Last edited by ahoodin; June 30th, 2006 at 10:43 AM.
    ahoodin
    To keep the plot moving, that's why.

  5. #5
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: AfxSocketInit() unnecessary for worker threads ?

    Wildfrog is absolutely correct. I too have seen many Internet postings that tell you to call AfxSocketInit once per thread. These postings are wrong. As shown by ahoodin, AfxSocketInit is a simple wrapper around WSAStartup, and the dos for this function are explicit in stating that WSAStartup need only be called once per process.

    Of course, calling it more than once will not hurt. So, you should be suspicious of other problems if you are calling it more than once and your app hangs etc.

    Mike

  6. #6
    Join Date
    Jun 2006
    Posts
    46

    Re: AfxSocketInit() unnecessary for worker threads ?

    Thanks all for explanations. My code constructs CSocket objects on heap and passes pointers to them between threads, so I reckon this is where the problem originates, however it does not hang if I use only one call to AfxSocketInit() in main thread. Cheers !

  7. #7
    Join Date
    Aug 1999
    Location
    <Classified>
    Posts
    6,882

    Re: AfxSocketInit() unnecessary for worker threads ?

    Quote Originally Posted by Cielak
    Thanks all for explanations. My code constructs CSocket objects on heap and passes pointers to them between threads, so I reckon this is where the problem originates, however it does not hang if I use only one call to AfxSocketInit() in main thread. Cheers !
    Are you detaching the socket before passing to other threaD ?

    Take a look at this,
    http://www.codeguru.com/forum/showthread.php?t=390429
    Regards,
    Ramkrishna Pawar

  8. #8
    Join Date
    Jun 2006
    Posts
    46

    Re: AfxSocketInit() unnecessary for worker threads ?

    yes. what i do is:

    all are worker threads

    - in main thread:
    i construct CSocket object on heap and declare equivalent SOCKET object

    - in second thread:
    declare CSocket pointer and point it at CSocket object just constructed in main thread, call Create() on that pointer, then Connect() or Listen() or Accept(). then i detach the CSocket pointer's handle to mentioned SOCKET object. then start another thread and second thread terminates.

    - in third thread:
    i construct CSocket object on heap and attach a mentioned CSOCKET object to it. then i delete CSocket object i constructed in main thread. then i point the leftover main thread's pointer at just constructed local CSocket object

    i know it is a mess, however thanks to this approach i am able to control worker threads from the main thread. having a global pointer that points to CSocket object allows me to cancel blocking call at any time and terminate worker thread without major memory leaks.
    next time though i will look into winapi sockets though, when it comes to synchronous sockets, as CSocket seems to be flawed.

  9. #9
    Join Date
    Aug 1999
    Location
    <Classified>
    Posts
    6,882

    Re: AfxSocketInit() unnecessary for worker threads ?

    Quote Originally Posted by Cielak
    yes. what i do is:

    all are worker threads

    - in main thread:
    i construct CSocket object on heap and declare equivalent SOCKET object

    - in second thread:
    declare CSocket pointer and point it at CSocket object just constructed in main thread, call Create() on that pointer, then Connect() or Listen() or Accept(). then i detach the CSocket pointer's handle to mentioned SOCKET object. then start another thread and second thread terminates.

    - in third thread:
    i construct CSocket object on heap and attach a mentioned CSOCKET object to it. then i delete CSocket object i constructed in main thread. then i point the leftover main thread's pointer at just constructed local CSocket object

    i know it is a mess, however thanks to this approach i am able to control worker threads from the main thread. having a global pointer that points to CSocket object allows me to cancel blocking call at any time and terminate worker thread without major memory leaks.
    next time though i will look into winapi sockets though, when it comes to synchronous sockets, as CSocket seems to be flawed.
    It's not a flaw but CSocket uses Thread Local Storage, and thats how it becomes thread dependant, I don't know why Microsoft has not included any proper documentation on this in MSDN.
    Regards,
    Ramkrishna Pawar

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured