How CAsyncSocket and its derived class objects are used across threads in MFC

Source: Internet
Author: User

How CAsyncSocket and its derived class objects are used across threads in MFC
The phenomenon of existence
In MFC in the development of WinSocket communication program with multithreading method, if you are the API way, naturally there is no question of the following said. But if you develop using CAsyncSocket and its derived classes (CSocket or your own), you will find that the program crashes when you use CAsyncSocket and its derived class objects in different threads. The cross-threading referred to here means that the object calls the Create/close/attach/detach function in one thread, and then calls other member functions such as receive/receivefrom/send/sendto in another thread. The following is a typical example program (fragment) that causes a crash:
CAsyncSocket Async_socket;

UINT ThreadProc (LPVOID)//Error: 1
{
TRACE ("======== threaddeattch Error: 1 ========/n");

Async_socket. Close (); The error occurred on this call: to close the Async_socket object.
return 0;
}

void Ctsasncsocketdlg::onbnclickedbuttoncreate ()//Error: 1
{
Async_socket. Create (0);//async_socket object is created in the main thread

:: AfxBeginThread (threadproc,0,0,0,0,0); The Async_socket object is passed to the other thread in the main path: ThreadProc
}

To know the cause of the error, you can follow in the analysis. This is too cumbersome to say, there are many similar articles on the Internet, the crux of the problem is not to make create/close calls in different threads.


First of all, when your program has a problem, how to judge it because of this problem? This is a key step. debugging/breakpoint/observation and so on a lot of means, VC + + IDE In this respect is very good. Here's an easy way to do it. For example, there is the following code:

Class Csocketp3pserver:public CAsyncSocket
{
Public
Csocketp3pserver ();
~ Csocketp3pserver ();

Public
.....//others

#if _DEBUG
Private
DWORD M_sockcurrentthreadid;
#endif//_DEUBG

};

void Csocketp3pserver::csocketp3pserver ()
{
#if _DEBUG
M_sockcurrentthreadid =:: GetCurrentThreadID ();
#endif//_debug
}

Csocketp3pserver::~csocketp3pserver ()
{
#if _DEBUG
if (M_sockcurrentthreadid! =:: GetCurrentThreadID ())
{
If the program comes here, it should be a cross-threading problem.
TRACE ("======= Csocketp3pserver object (address:%d) called ========/n" in different threads, this);
}
#endif//_debug
}

Know the problem, the solution is very good, there are a lot of related articles on the Internet. Here is the solution to the arrangement.

There are 2 main ideas to solve:
1: Avoid this problem, that is to say, avoid using CAsyncSocket and its derived class objects across threads.
There are a lot of tricks about this, such as a thread that can be used to send and receive jobs, while other threads pass data sharing or other means to each other. This is the relationship between a < reader-author >. There are other ways, in short, to put the socket object's entire lifetime on a single thread.

2: There is really no way to avoid using CAsyncSocket and its derived class objects in cross-threading, which can be achieved by passing sockets < variables > between threads, here is an example of the following code:

UINT Threaddeattchme (LPVOID PV)//correct use of as_socket across threads, here is worker thread A
{
TRACE ("======== threaddeattch correct use of socket ========/n across threads");

As_socket. Attach (SOCKET) PV); <----------Note that this is the SOCKET < variable >

As_socket. Close ();

return 0;
}

void Ctsasncsocketdlg::onbnclickedbuttoncreate ()//correct in cross-threading using the socket, here is the main thread
{
As_socket. Create (0);
AfxBeginThread (Threaddeattchme, (LPVOID) as_socket. Detach (), 0,0,0,0);//<----------Note that this is the SOCKET < variable >
}

How CAsyncSocket and its derived class objects are used across threads in MFC

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.