Accept and Multi-Threading problem!!!

Posted on 1998-12-28
Last Modified: 2013-11-20
hi folks,

    I am building a server application using CSocket class and threads. In my server app, I create a child thread (worker thread) to handle connection (Accept()) and network I/O (infinite loop). I also create a  exit message handler to closes all network connections. Whenever I click the exit button on my server app, my program crashed. Why?? Any solution??

Question by:stephenkong
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 86

Expert Comment

ID: 1326835
Do you synchronize the access to the CSockets between the threads and the message handler?

Expert Comment

ID: 1326836
Adding to jkr's response, is the worker thread accessing any objects that are created by another therad (stack or heap).   If so, when your primary (MH) thread dies and destroys those objects, access over.

Expert Comment

ID: 1326837
While the win32 api is thread safe, MFC isn't. You should sinchronize all access to MFC objects yourself.
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 86

Expert Comment

ID: 1326838
>>While the win32 api is thread safe...
shaig, i wish it was! ;-)
(just try 2 threads accessing one GDI object without synchronization...)

Expert Comment

ID: 1326839
My program Crash answer !??

add some code, maybe this will help ?!

Expert Comment

ID: 1326840
Using handles?  Well if you say so i'll take your word for it. When you think you saw it all, Microsoft come and...
LVL 86

Expert Comment

ID: 1326841
shaig - it is so. Somewhere deep in the docs they state that the GDI isn't sync'ed due to performance issues ;-)

Author Comment

ID: 1326842
hello folks,

   My worker thread was executing CSocket::Accept() while I was pressing the Exit button.

   In the 'Exit' message handler:
// This variable is useless since worker thread was stuck in
// CSocket::Accept()
   m_bIsFinished = TRUE;  // bool variable to break the infinite
                          // loop inside the worker thread
   PostQuitMessage (0);

Expert Comment

ID: 1326843
When the main thread close the socket of worker thread, if the worker thread is still using the thread and error handling is not carefully done, this might happen.

Accepted Solution

jonalee earned 50 total points
ID: 1326844
Please do not use CSocket::Close() directly. Use Shutdown(2) first. Then overide the CAsyncSocket::OnClose(int). In that function, call Close() after CAsyncSocket::Close() is called.

Also, if the coding you give out is in the Thread, please use PostThreadMessage(WM_QUIT,0,0L)


Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
function returning CString in a regular win32 DLL 8 215
Pressing cancel button with C# UI Automation 3 104
I could not set window to top 4 62
Adjust the codes 3 63
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question