Jump to content
  • Advertisement
Sign in to follow this  
crazy_andy

passing parrameters to a new thread

This topic is 5094 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
The function prototype:

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;
}


Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!