473,399 Members | 3,401 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 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 7240
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

31
by: AlexeiOst | last post by:
Everywhere in documentation there are recommendations to use threads from thread pooling for relatively short tasks. As I understand, fetching a page or multiple pages (sometimes up to 50 but not...
2
by: n_o_s_p_a__m | last post by:
Hey all, which program(s) should I use and what should the size be for desiging a new ico file which will be a notifyicon?
7
by: Charles Law | last post by:
My first thought was to call WorkerThread.Suspend but the help cautions against this (for good reason) because the caller has no control over where the thread actually stops, and it might have...
3
by: JohnnyGr | last post by:
I have heard theres a new way to start threads with parameters in framework 2.0, does anyone know how to do that? this is what i need to do... Start a thread that executes some stuff, in this...
11
by: seattleboatguy | last post by:
I am trying to send a message from a visual c++ user-thread to the main window, so the main window can update text on the window to reflect what the user-thread is doing. I have tried 2 different...
4
by: Joachim | last post by:
When setting a breakpoint in a thread that I have created and the debugger reaches that point, Visual Studio just hangs for about 10 seconds and then, when I try F10 the thread ends and the debug...
7
by: Liz | last post by:
My ListView components seem to produce distortion when they are scrolled in Details view; it looks almost like the text has a "strike-through" attribute applied to it. Does anyone know of a fix...
40
by: Angus | last post by:
Hello I am writing a library which will write data to a user defined callback function. The function the user of my library will supply is: int (*callbackfunction)(const char*); In my...
8
by: raylopez99 | last post by:
I have the latest version of Visual Studio 2008 Professional, which allows you to create resource files (this is the .resx file, no?), unlike the Express version, which does not. I am trying to...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.