passing parrameters to a new thread
using AfxBeginThread()
I need to pass the array c[2] to a new thread. How can I do this, I have googled it but not found anything clear.
The function prototype:
One of the parameters is
An example to illustrate:
When the thread is running, it will have access to the data you passed as a parameter, but remember to use it, you'll need to cast it to the relevant type:
CWinThread* AfxBeginThread( AFX_THREADPROC pfnThreadProc, LPVOID pParam, int nPriority = THREAD_PRIORITY_NORMAL, UINT nStackSize = 0, DWORD dwCreateFlags = 0, LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL );
One of the parameters is
LPVOID pParam
, which allows the user to pass one parameter of any type to the thread. So, in order to pass multiple parameters, you will need to create a struct (or class) to hold the parameters and then pass the address of an instance of this data to the function instead.An example to illustrate:
struct myThreadParams{ int c[2]; int ca[2]; char *someString;};// Some of your code:myThreadParams *params = new myThreadParams;params->someString = "hello";params->c[0] = 1; params->c[3] = 2;// Pass the param to the thread;CWinThread *thread = AfxBeginThread( &MyThreadProc, params, /* rest are default values */ );
When the thread is running, it will have access to the data you passed as a parameter, but remember to use it, you'll need to cast it to the relevant type:
UINT MyThreadProc( LPVOID pParam ){ myThreadParams *params = static_cast<myThreadParams *>( pParam ); MessageBox( params->someString ); return 0;}
cheers, by the way, what is the cast for a socket. as in SOCKET c[2].
edit: rather than create a new thread, can someone show me how to do this please:
I have
char temp[512];
cin >> temp; //user types in exit
if (temp == "exit")
{
...
}
return;
I know this is basic, but I can't remember it. When I run this and type exit it doesn't do anything, it just skips, and Im not sure why.
edit: rather than create a new thread, can someone show me how to do this please:
I have
char temp[512];
cin >> temp; //user types in exit
if (temp == "exit")
{
...
}
return;
I know this is basic, but I can't remember it. When I run this and type exit it doesn't do anything, it just skips, and Im not sure why.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement