Thread: _beginthread with std::string params

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    932

    _beginthread with std::string params

    Im trying to call _beginthread with multiple arguments but I get undefined behavior.
    It either wont be called or just idles forever without printing out anything.

    Code:
    #define WIN32_LEAN_AND_MEAN
    #include <windows.h>
    #include <process.h>
    #include <string>
    #include <iostream>
    using namespace std;
    
    typedef struct
    {
        string data1;
        string data2;
    } t;
    
    t arg;
    
    void Thread(void *param)
    {
        cout << " Thread \n";
        t *args = (t*) param;
        string x = args->data1;
        string y = args->data2;
    
        cout << x << y << " \n";
    
       _endthread();
    }
    
    void foo(string a, string b)
    {
        arg.data1 = a;
        arg.data2 = b;
        HANDLE handle = (HANDLE) _beginthread(Thread, 0, (void*) &arg);
        cout << " foo " << handle << "\n";
    }
    
    int main()
    {
        foo("Hello"," World");
    
        return 0;
    }
    Using Windows 10 with Code Blocks and MingW.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    When main ends, the constructor of your global variable is called, leading to undefined behavior. You must block until Thread is finished with its data.
    But why aren't you using std::thread instead? Regardless, you must still block until your thread is complete as when main exits, all your threads die.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Thanks, that makes sense.

    I was having problem with std::thread in a Win32 loop. But now I discovered thread.detach(); and I do use std::thread.
    Using Windows 10 with Code Blocks and MingW.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Elysia View Post
    When main ends, the constructor of your global variable is called, leading to undefined behavior. You must block until Thread is finished with its data.
    But why aren't you using std::thread instead? Regardless, you must still block until your thread is complete as when main exits, all your threads die.
    Typo: constructor should be destructor.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    It's even worse than main ending.

    > cout << " foo " << handle << "\n";
    arg is going out of scope at this point, so the &arg you passed to the thread is already off in the weeds somewhere.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    arg is a global variable, though? How can it go out of scope at the end of foo?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Mis-read
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yeah, I thought arg was a local variable the first time I read the code too. Probably because it sounds like a local variable.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Create a Window from _beginthread
    By matth in forum C++ Programming
    Replies: 0
    Last Post: 03-15-2006, 04:07 PM
  2. _beginthread()
    By matth in forum C++ Programming
    Replies: 4
    Last Post: 08-17-2005, 07:39 PM
  3. problem with _beginthread
    By osal in forum Windows Programming
    Replies: 6
    Last Post: 07-18-2004, 04:45 PM
  4. _beginthread
    By asafze in forum C Programming
    Replies: 2
    Last Post: 06-17-2004, 12:49 PM
  5. _beginthread when function has more than one parameter
    By blundstrom in forum C Programming
    Replies: 5
    Last Post: 10-04-2001, 12:28 PM