Home > Software engineering >  How to shut off the child thread is blocked
How to shut off the child thread is blocked

Time:11-12

Child thread is in the network connection, and then I have been waiting for receiving data, can realize receiving function normally, but complains when shut down the program memory leaks, guess the reason is that the recv, is a blocking function, in the main program onclose send the mark of a closed subroutine and the custom closed messages sent in two ways, can't enter into the child thread closed, also tried to sleep time, do you have any good ways, shut off the child thread is blocked?

UINT MyThreadProc (LPVOID pParam)
{
Char buff [2000].
int n;
The SOCKET Client=((RECVPARAM *) pParam) - & gt; The sock.
HWND HWND=((RECVPARAM *) pParam) - & gt; The HWND;
SOCKADDR_IN Addr=((RECVPARAM *) pParam) - & gt; Addr.

If (SOCKET_ERROR==connect (Client, (SOCKADDR *) & amp; Addr, sizeof (SOCKADDR)))
{
Int nError=WSAGetLastError ();
Cstrings strErrorMsg;
SendMessage (hWnd WM_NETERROR, 0, 0);
return FALSE;
}
: : SendMessage (hWnd WM_NETCONNECTED, 0, 0);//network connection success

While (TRUE)
{
Memset (buff, 0, sizeof (buff));
N=recv (Client, buff, sizeof (buff), 0).//jam, n is number of bytes to copy
If (n==0)//network connection is disconnected, such as turning off the server
{
SendMessage (hWnd WM_NETERROR, 0, 0);
return 0;
}
If (n==SOCKET_ERROR)
{
SendMessage (hWnd WM_NETERROR, 0, 0);
return 0;
}
: : SendMessage (hWnd WM_RECVDATA, (WPARAM) n (LPARAM) buff);
}
return TRUE;
}

CodePudding user response:

Is the best way to the end of the cycle, exit the thread, have a logo, the child thread met mark would quit, all threads are out of the process and then exit the main
If (theApp. M_mutexExit. IsLocked ())//is quit, interrupt
return;

Can also be forced to kill the thread
M_pThread - & gt; M_bAutoDelete=TRUE;
DWORD ExitCode;
GetExitCodeThread (m_pThread - & gt; M_hThread, & amp; ExitCode);
The TerminateThread (m_pThread - & gt; M_hThread ExitCode);

CodePudding user response:

Forced to kill the thread will cause memory leaks

Detected the memory leaks!
Dumping objects - & gt;
F: \ dd \ vctools \ vc7libs \ ship \ atlmfc \ SRC \ MFC \ thrdcore CPP (306) : {369} client block at 0 x00d45dd8, subtype c0, 68 bytes long.
F: \ dd \ vctools \ vc7libs \ ship \ atlmfc \ SRC \ MFC \ dumpcont CPP (23) : atlTraceGeneral - a CWinThread object at $00 d45dd8, 68 bytes long

I set up a global symbol, will sign on to close the main window is set to true, in the while judging marks that we will return to true, but debugging flag bits set a success, but don't enter into the child thread go directly out of the program, is a memory leak

CodePudding user response:


Close the socket.

CodePudding user response:

reference smwhotjay reply: 3/f

Elegant close the socket.
can you tell me specific how to operate?

CodePudding user response:

Recommend ioctlsocket FIONBIO asynchronous mode,
This is a chance to test out the tag

CodePudding user response:

The main thread to add a line
The WaitForSingleObject (m_pThread - & gt; M_hThread, 500);//waiting thread ends
  • Related