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