Home > Software engineering >  How in a member function call CreateThread another member function called?
How in a member function call CreateThread another member function called?

Time:10-05

 UINT MFCDlg server: C: recv1 (SOCKET a1) 
{
While (TRUE)
{
SOCKADDR Client1.
Int size=sizeof (SOCKADDR);
Char * MSG="123456786456123 \ r \ n";
The SOCKET Client=accept (a1, & amp; Client1, & amp; The size);
Send (Client, MSG, strlen (MSG) + sizeof (char), NULL);
While (TRUE)
{
The recv (Client, (char *) & amp; Msg2 MAXBYTE, NULL);

}

}
}
Void MFCDlg server: C: OnBnClickedButton1 ()
{
HANDLE Thread1=CreateThread (NULL, 0, (LPTHREAD_START_ROUTINE) recv1, (LPVOID) a1, 0, 0).
While (1)
{
The UpdateData (FALSE);
}

Closesocket (a1);
WSACleanup ();

}


And I also need to put the information obtained from the SOCKET (string), update to the MFC edit box msg1 cstrings, but there was a dilemma N
the condition of hard1. If you don't set recv1 to static member function can't call, but set a static member function the caller as a static member function can't access the static member variables MSG
2. My in the mind have millions of grass mud horse in the pentium!

CodePudding user response:

Start a thread, will be handed in this pointer is not over?
HANDLE Thread1=CreateThread (NULL, 0, (LPTHREAD_START_ROUTINE) recv1, (LPVOID) this, 0, 0).

CodePudding user response:

Thread parameter passing in the object pointer, and then call the corresponding function through this pointer

CodePudding user response:

Still have to pay attention to add the necessary lock,

CodePudding user response:

Have responded to the front, the form object (this) as a parameter passed to the thread function.

CodePudding user response:

refer to the second floor oyljerry response:
object pointer as the thread parameters passed in, then call the corresponding function through this pointer


The parameters of this pointer passed in, recv1 a1 how to get in

CodePudding user response:

You can customize a structure, put inside you need to pass to the thread function members, such as
Typedef struct _tagPARAMINFO
{
The SOCKET sock;
The HWND HWND;
.
} PARAMINFO;

CodePudding user response:

Multiple threads to the same socket call the send is the rhythm of the fault,
  • Related