Solved

Accept and Multi-Threading problem!!!

Posted on 1998-12-28
10
226 Views
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??

stephen
 
0
Comment
Question by:stephenkong
10 Comments
 
LVL 86

Expert Comment

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

Expert Comment

by:yoffe
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 violation...game over.
0
 
LVL 3

Expert Comment

by:shaig
ID: 1326837
While the win32 api is thread safe, MFC isn't. You should sinchronize all access to MFC objects yourself.
0
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 86

Expert Comment

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

Expert Comment

by:koskia
ID: 1326839
My program Crash answer !??

add some code, maybe this will help ?!
0
 
LVL 3

Expert Comment

by:shaig
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...
0
 
LVL 86

Expert Comment

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

Author Comment

by:stephenkong
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
   Sleep(300);    
   sock.Close();
   PostQuitMessage (0);
}
0
 
LVL 7

Expert Comment

by:faster
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.
0
 
LVL 2

Accepted Solution

by:
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)


0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Get filename and folder into excel 7 76
notReplace  challenge 53 131
Hibernate methods 2 73
centeredAverage challenge 8 167
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.

831 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