Create Thread and parameter distortion

Discussion in 'C++' started by AMS, Jun 27, 2003.

  1. AMS

    AMS Guest

    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_ROUTINE)StartScript,
    &commandline,
    0,&threadID);
    return;
    }

    Any ideas?

    Thanks!
    AMS, Jun 27, 2003
    #1
    1. Advertising

  2. "AMS" <> 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_ROUTINE)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
    Victor Bazarov, Jun 27, 2003
    #2
    1. Advertising

  3. In article <xf0La.7197$>, Sin
    <> 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_ROUTINE)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_ROUTINE)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_commandline)));
    }

    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
    Howard Hinnant, Jun 27, 2003
    #3
  4. AMS

    AMS Guest

    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
    AMS, Jul 1, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lialie - KingMax
    Replies:
    1
    Views:
    297
    John Henry
    Dec 15, 2006
  2. Replies:
    4
    Views:
    518
    Matteo
    Mar 29, 2007
  3. Stuart Redmann
    Replies:
    5
    Views:
    476
    Stuart Redmann
    Dec 14, 2007
  4. Replies:
    5
    Views:
    630
  5. Anti Log
    Replies:
    12
    Views:
    131
    jmfauth
    May 23, 2013
Loading...

Share This Page