471,072 Members | 1,394 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,072 software developers and data experts.

Create Thread and parameter distortion

AMS
I am trying to create a thread to execute a script for me and then
display the output in notepad.

What is happening is when I call the CreateThread method and pass in
the Routine and Parameter the parameter that is being passed is
distorted when it reaches the routine. If I output the parameter in
the routine it is just garbage. It works fine on Win NT but not in Win
2000.

Here is my method that calls CreateThread

void CTestDlg::BeginScriptOnNewThread()
{
DWORD threadID;
char commandline[1000];

strcpy(commandline, m_commandline);

HANDLE ThreadHandle=CreateThread(0,0,(LPTHREAD_START_ROUT INE)StartScript,
&commandline,
0,&threadID);
return;
}

Any ideas?

Thanks!
Jul 19 '05 #1
3 7108
"AMS" <ab**@cfu.net> wrote...
I am trying to create a thread to execute a script for me and then
display the output in notepad.

What is happening is when I call the CreateThread method and pass in
the Routine and Parameter the parameter that is being passed is
distorted when it reaches the routine. If I output the parameter in
the routine it is just garbage. It works fine on Win NT but not in Win
2000.

Here is my method that calls CreateThread

void CTestDlg::BeginScriptOnNewThread()
{
DWORD threadID;
char commandline[1000];

strcpy(commandline, m_commandline);

HANDLE ThreadHandle=CreateThread(0,0,(LPTHREAD_START_ROUT INE)StartScript,
&commandline,
0,&threadID);
return;
}

Any ideas?


'commandline' is a local variable. By the time a new thread is
created the memory occupied by 'commandline' is either GONE
because the function 'BeginScriptOnNewThread' has finished and
all local variables have ceased to exist, or because your thread
has different stack (the former is more likely) and 'commandline'
pointer is not a valid pointer or points to something else there.

You have to copy 'commandline' into a global variable so that
the thread will be able to access it.

Victor
Jul 19 '05 #2
In article <xf*******************@news20.bellglobal.com>, Sin
<br****@hotmail.com> wrote:

| > I am trying to create a thread to execute a script for me and then
| > display the output in notepad.
| >
| > What is happening is when I call the CreateThread method and pass in
| > the Routine and Parameter the parameter that is being passed is
| > distorted when it reaches the routine. If I output the parameter in
| > the routine it is just garbage. It works fine on Win NT but not in Win
| > 2000.
| >
| > Here is my method that calls CreateThread
| >
| > void CTestDlg::BeginScriptOnNewThread()
| > {
| > DWORD threadID;
| > char commandline[1000];
| >
| > strcpy(commandline, m_commandline);
| >
| > HANDLE ThreadHandle=CreateThread(0,0,(LPTHREAD_START_ROUT INE)StartScript,
| > &commandline,
| > 0,&threadID);
| > return;
| > }
| >
| > Any ideas?
|
|
| Well CreateThread can return before the thread actually gets any time...
| This means there is a possibility that commandline isn't allocated anymore
| by the time you use it in the thread (it doesnt exist anymore by the time
| you exit BeginScriptOnNewThread(). There are many ways around this, one of
| them being :
|
| void CTestDlg::BeginScriptOnNewThread()
| {
| DWORD threadID;
| char *commandline= new char[1000];
| strcpy(commandline, m_commandline);
|
| HANDLE
| ThreadHandle=CreateThread(0,0,(LPTHREAD_START_ROUT INE)StartScript,
| commandline, 0, &threadID);
|
| return;
| }
|
| *** In the thread routine, make sure you use delete on the pointer when done
| with it ***
|
|
| If the thread is garanteed to be done by the time your dialog is closed,
| then you can skip the copy altogether and use &m_commandline directly,
| assuming neither the thread or dialog modify this variable...
|
| If the param is only used to initialize the thread, you could also use
| synchronisation... You could wait for the thread to signal that it's safe to
| return, before using return in the function that creates the thread...
|
| As for why it works in NT, well I suppose it's just luck... Probably a very
| subtle difference in the scheduling or thread initialization mechanism or
| something... Thing is it can or cannot work from one execution to the
| next... But it's always wrong, even when it works, since you're using memory
| that you very probably do not own...

You might take a look at the boost thread and function libs
(www.boost.org). The combination takes care of this problem very
nicely.

You might code it up like:

struct hold_command
{
hold_command(const char* command)
{
strcpy(m_commandline, command);
}

void operator()()
{
StartScript(m_commandline);
}
private:
char m_commandline[1000];
};

void CTestDlg::BeginScriptOnNewThread()
{
thread threadID(function<void()>(hold_command(m_commandli ne)));
}

The boost thread starter function doesn't take any parameters. But as
you can see it is easy to create a functor that converts a function
that takes a parameter into one that doesn't. And then the thread
constructor takes care of the lifetime issues for you. It insures that
the new thread is off and running before the functor used to start it
destructs.

--
Howard Hinnant
Metrowerks
Jul 19 '05 #3
AMS
Alex -
Thank you so much for your response! It was much appreciated. I didn't
write the code - just have to fix it so I probably wouldn't have come
to this realization on my own - still new to C++. I ended up just
waiting for the thread to complete before returning and now it works
great! :)

Thanks again,
Abby
Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

31 posts views Thread by AlexeiOst | last post: by
2 posts views Thread by n_o_s_p_a__m | last post: by
7 posts views Thread by Charles Law | last post: by
11 posts views Thread by seattleboatguy | last post: by
7 posts views Thread by Liz | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.