Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Create Thread and parameter distortion

Reply
Thread Tools

Create Thread and parameter distortion

 
 
AMS
Guest
Posts: n/a
 
      06-27-2003
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!
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      06-27-2003
"AMS" <(E-Mail Removed)> 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


 
Reply With Quote
 
 
 
 
Howard Hinnant
Guest
Posts: n/a
 
      06-27-2003
In article <xf0La.7197$(E-Mail Removed)>, Sin
<(E-Mail Removed)> 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
 
Reply With Quote
 
AMS
Guest
Posts: n/a
 
      07-01-2003
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Siemens Gigaset heads up - One way audio and distortion Dave Saville UK VOIP 2 10-13-2008 03:12 PM
Lens perspective distortion and DSLR crop factor pgmacdonald@gmail.com Digital Photography 10 05-21-2007 09:23 AM
Is it good to create a thread in a non gui thread? Lialie - KingMax Python 1 12-15-2006 05:35 AM
desk top distortion george lewis Computer Support 5 11-28-2003 07:35 PM
jpg and gif distortion on webpage Larry Smith Computer Support 13 11-20-2003 09:57 PM



Advertisments