Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Accept and Multi-Threading problem!!!

Posted on 1998-12-28
10
Medium Priority
?
231 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
[X]
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
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

Question has a verified solution.

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

If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

721 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