Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Accept and Multi-Threading problem!!!

Posted on 1998-12-28
10
Medium Priority
?
232 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 150 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

Industry Leaders: 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!

Question has a verified solution.

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

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
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.
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…

824 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