Passing parameters to _beginthreadex












1















I'm attempting to do some basic parallelisation using _beginthreadex, and passing parameters as per an example I was given, but it won't work.



Any ideas?



#include <iostream> 
#include <process.h>



void MyThread(void *data)
{
std::cout << "Hello World!";
}

int main()
{
_beginthreadex(NULL, 0, MyThread, NULL, 0, NULL);
while(true);
}


EDIT:



Why won't passing NULL as an argument work? (Since the function takes no arguments anyway?)



Passing NULL as an arguments list worked fine with _beginthread.










share|improve this question

























  • what does it do? How doesn't it work?

    – forsvarir
    May 11 '11 at 17:18













  • error C2664: '_beginthreadex' : cannot convert parameter 3 from 'void (__cdecl *)(void *)' to 'unsigned int (__stdcall *)(void *)'

    – Eilidh
    May 11 '11 at 17:28











  • apparently your function needs to return an integer. See my edited post.

    – Constantinius
    May 11 '11 at 17:35


















1















I'm attempting to do some basic parallelisation using _beginthreadex, and passing parameters as per an example I was given, but it won't work.



Any ideas?



#include <iostream> 
#include <process.h>



void MyThread(void *data)
{
std::cout << "Hello World!";
}

int main()
{
_beginthreadex(NULL, 0, MyThread, NULL, 0, NULL);
while(true);
}


EDIT:



Why won't passing NULL as an argument work? (Since the function takes no arguments anyway?)



Passing NULL as an arguments list worked fine with _beginthread.










share|improve this question

























  • what does it do? How doesn't it work?

    – forsvarir
    May 11 '11 at 17:18













  • error C2664: '_beginthreadex' : cannot convert parameter 3 from 'void (__cdecl *)(void *)' to 'unsigned int (__stdcall *)(void *)'

    – Eilidh
    May 11 '11 at 17:28











  • apparently your function needs to return an integer. See my edited post.

    – Constantinius
    May 11 '11 at 17:35
















1












1








1


1






I'm attempting to do some basic parallelisation using _beginthreadex, and passing parameters as per an example I was given, but it won't work.



Any ideas?



#include <iostream> 
#include <process.h>



void MyThread(void *data)
{
std::cout << "Hello World!";
}

int main()
{
_beginthreadex(NULL, 0, MyThread, NULL, 0, NULL);
while(true);
}


EDIT:



Why won't passing NULL as an argument work? (Since the function takes no arguments anyway?)



Passing NULL as an arguments list worked fine with _beginthread.










share|improve this question
















I'm attempting to do some basic parallelisation using _beginthreadex, and passing parameters as per an example I was given, but it won't work.



Any ideas?



#include <iostream> 
#include <process.h>



void MyThread(void *data)
{
std::cout << "Hello World!";
}

int main()
{
_beginthreadex(NULL, 0, MyThread, NULL, 0, NULL);
while(true);
}


EDIT:



Why won't passing NULL as an argument work? (Since the function takes no arguments anyway?)



Passing NULL as an arguments list worked fine with _beginthread.







c++ windows multithreading






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 11 '11 at 17:24







Eilidh

















asked May 11 '11 at 17:16









EilidhEilidh

66331638




66331638













  • what does it do? How doesn't it work?

    – forsvarir
    May 11 '11 at 17:18













  • error C2664: '_beginthreadex' : cannot convert parameter 3 from 'void (__cdecl *)(void *)' to 'unsigned int (__stdcall *)(void *)'

    – Eilidh
    May 11 '11 at 17:28











  • apparently your function needs to return an integer. See my edited post.

    – Constantinius
    May 11 '11 at 17:35





















  • what does it do? How doesn't it work?

    – forsvarir
    May 11 '11 at 17:18













  • error C2664: '_beginthreadex' : cannot convert parameter 3 from 'void (__cdecl *)(void *)' to 'unsigned int (__stdcall *)(void *)'

    – Eilidh
    May 11 '11 at 17:28











  • apparently your function needs to return an integer. See my edited post.

    – Constantinius
    May 11 '11 at 17:35



















what does it do? How doesn't it work?

– forsvarir
May 11 '11 at 17:18







what does it do? How doesn't it work?

– forsvarir
May 11 '11 at 17:18















error C2664: '_beginthreadex' : cannot convert parameter 3 from 'void (__cdecl *)(void *)' to 'unsigned int (__stdcall *)(void *)'

– Eilidh
May 11 '11 at 17:28





error C2664: '_beginthreadex' : cannot convert parameter 3 from 'void (__cdecl *)(void *)' to 'unsigned int (__stdcall *)(void *)'

– Eilidh
May 11 '11 at 17:28













apparently your function needs to return an integer. See my edited post.

– Constantinius
May 11 '11 at 17:35







apparently your function needs to return an integer. See my edited post.

– Constantinius
May 11 '11 at 17:35














5 Answers
5






active

oldest

votes


















7














Your code has two errors in it, neither of which are related to the parameter to the thread function --- NULL is fine for that, as you surmised.



The problems are in the signature of the thread function, and the error you are getting points this out. Firstly, it must be a __stdcall function, and secondly it must return an unsigned int. Your function is __cdecl and returns void.



unsigned __stdcall MyThread(void *data)
{
std::cout << "Hello World!";
return 0;
}


should fix the problem for you.






share|improve this answer































    4














    obviously in your code you are not passing any parameter. To pass a variable you have to do the following (for example):



    #include <iostream> 
    #include <process.h>

    void MyThread(void *data)
    {
    int x = static_cast<int*>(data);
    std::cout << "Hello World! " << x;
    }

    int main()
    {
    int x = 10;
    _beginthreadex(NULL, 0, MyThread, &x, 0, NULL);
    while(true);
    }


    UPDATE: Since you posted the compilation problem later:
    apparently your thread function needs to return an integer:



    int MyThread(void *data)
    {
    std::cout << "Hello World!";
    return 0;
    }





    share|improve this answer


























    • But no variable needs to be passed since MyThread accepts no arguments?

      – Eilidh
      May 11 '11 at 17:20











    • It worked with NULL as the arguments list in _beginthread.

      – Eilidh
      May 11 '11 at 17:21











    • A it does have an argument list a void* and B yes because NULL is a valid void * value.

      – rerun
      May 11 '11 at 17:23













    • So NULL should be acceptable?

      – Eilidh
      May 11 '11 at 17:24











    • of course, the code you wrote is acceptable and propably compilable. But I thought you wanted to pass a parameter, or didn't you?

      – Constantinius
      May 11 '11 at 17:33



















    1














    You pass your data through the fourth parameter. This passes a pointer to i:



    unsigned __stdcall thread(void *arg)
    {
    int *iptr = (int*)arg;
    ...
    }

    int i;
    _beginthreadex(0, 0, thread, &i, 0, 0);


    Note that the thread function signature I used here is different than what you use: I return an unsigned and use the __stdcall calling convention -- this is the signature _beginthreadex expects.



    Depending on what you're trying to do, the new Concurrency Runtime features in VC++ might be simpler to use than explicitly managing your own threads.



    Edit in response to question edit:



    You can pass any valid void pointer, including NULL. If you do that, you can even leave out the parameter's name since you're not using it:



    unsigned __stdcall thread(void*)
    {
    ...
    }

    _beginthreadex(0, 0, thread, 0, 0, 0);





    share|improve this answer


























    • your function does not return any value. this will most certainly lead to a compiler warning/error.

      – Constantinius
      May 11 '11 at 17:38











    • Yes, I left that out for brevity. You also of course shouldn't try to call _beginthreadex from outside of a function!

      – Cory Nelson
      May 11 '11 at 17:45



















    0














    The argument to _beginthreadex must be a function with the __stdcall calling convention. Your function has __cdecl. A simple insertion of __stdcall at the correct spot should solve the problem.



    void __stdcall MyThread(void *data)
    {
    std::cout << "Hello World!";
    }





    share|improve this answer
























    • I added that in, but I'm still getting this error - error C2664: '_beginthreadex' : cannot convert parameter 3 from 'void (__stdcall *)(void *)' to 'unsigned int (__stdcall *)(void *)'

      – Eilidh
      May 11 '11 at 17:32



















    0














    The fourth parameter in _beginthreadex(NULL, 0, MyThread, NULL, 0, NULL) are arguments to the function MyThread



    Right now you are passing NULL to it so void* data must be getting a NULL pointer



    struct X  
    {
    int a;
    int b;
    };

    X* x = (X*)malloc(sizeof(X));

    x->a = 5;
    x->b = 6;
    _beginthreadex(NULL, 0, MyThread, NULL, x, NULL);


    The above code will pass pointer x to the function MyThread().
    Do take care of X, as it better should be allocated by malloc or new. Do not create it on stack until you use a thread join






    share|improve this answer


























    • NULL is a valid void* type isn't it?

      – atoMerz
      May 11 '11 at 17:29











    • @Atomerz: yeah you are right. I misunderstood the question and did not see the EDIT part of the question

      – Mayank
      May 11 '11 at 17:59











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f5968076%2fpassing-parameters-to-beginthreadex%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    5 Answers
    5






    active

    oldest

    votes








    5 Answers
    5






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    7














    Your code has two errors in it, neither of which are related to the parameter to the thread function --- NULL is fine for that, as you surmised.



    The problems are in the signature of the thread function, and the error you are getting points this out. Firstly, it must be a __stdcall function, and secondly it must return an unsigned int. Your function is __cdecl and returns void.



    unsigned __stdcall MyThread(void *data)
    {
    std::cout << "Hello World!";
    return 0;
    }


    should fix the problem for you.






    share|improve this answer




























      7














      Your code has two errors in it, neither of which are related to the parameter to the thread function --- NULL is fine for that, as you surmised.



      The problems are in the signature of the thread function, and the error you are getting points this out. Firstly, it must be a __stdcall function, and secondly it must return an unsigned int. Your function is __cdecl and returns void.



      unsigned __stdcall MyThread(void *data)
      {
      std::cout << "Hello World!";
      return 0;
      }


      should fix the problem for you.






      share|improve this answer


























        7












        7








        7







        Your code has two errors in it, neither of which are related to the parameter to the thread function --- NULL is fine for that, as you surmised.



        The problems are in the signature of the thread function, and the error you are getting points this out. Firstly, it must be a __stdcall function, and secondly it must return an unsigned int. Your function is __cdecl and returns void.



        unsigned __stdcall MyThread(void *data)
        {
        std::cout << "Hello World!";
        return 0;
        }


        should fix the problem for you.






        share|improve this answer













        Your code has two errors in it, neither of which are related to the parameter to the thread function --- NULL is fine for that, as you surmised.



        The problems are in the signature of the thread function, and the error you are getting points this out. Firstly, it must be a __stdcall function, and secondly it must return an unsigned int. Your function is __cdecl and returns void.



        unsigned __stdcall MyThread(void *data)
        {
        std::cout << "Hello World!";
        return 0;
        }


        should fix the problem for you.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered May 11 '11 at 21:48









        Anthony WilliamsAnthony Williams

        52k8102139




        52k8102139

























            4














            obviously in your code you are not passing any parameter. To pass a variable you have to do the following (for example):



            #include <iostream> 
            #include <process.h>

            void MyThread(void *data)
            {
            int x = static_cast<int*>(data);
            std::cout << "Hello World! " << x;
            }

            int main()
            {
            int x = 10;
            _beginthreadex(NULL, 0, MyThread, &x, 0, NULL);
            while(true);
            }


            UPDATE: Since you posted the compilation problem later:
            apparently your thread function needs to return an integer:



            int MyThread(void *data)
            {
            std::cout << "Hello World!";
            return 0;
            }





            share|improve this answer


























            • But no variable needs to be passed since MyThread accepts no arguments?

              – Eilidh
              May 11 '11 at 17:20











            • It worked with NULL as the arguments list in _beginthread.

              – Eilidh
              May 11 '11 at 17:21











            • A it does have an argument list a void* and B yes because NULL is a valid void * value.

              – rerun
              May 11 '11 at 17:23













            • So NULL should be acceptable?

              – Eilidh
              May 11 '11 at 17:24











            • of course, the code you wrote is acceptable and propably compilable. But I thought you wanted to pass a parameter, or didn't you?

              – Constantinius
              May 11 '11 at 17:33
















            4














            obviously in your code you are not passing any parameter. To pass a variable you have to do the following (for example):



            #include <iostream> 
            #include <process.h>

            void MyThread(void *data)
            {
            int x = static_cast<int*>(data);
            std::cout << "Hello World! " << x;
            }

            int main()
            {
            int x = 10;
            _beginthreadex(NULL, 0, MyThread, &x, 0, NULL);
            while(true);
            }


            UPDATE: Since you posted the compilation problem later:
            apparently your thread function needs to return an integer:



            int MyThread(void *data)
            {
            std::cout << "Hello World!";
            return 0;
            }





            share|improve this answer


























            • But no variable needs to be passed since MyThread accepts no arguments?

              – Eilidh
              May 11 '11 at 17:20











            • It worked with NULL as the arguments list in _beginthread.

              – Eilidh
              May 11 '11 at 17:21











            • A it does have an argument list a void* and B yes because NULL is a valid void * value.

              – rerun
              May 11 '11 at 17:23













            • So NULL should be acceptable?

              – Eilidh
              May 11 '11 at 17:24











            • of course, the code you wrote is acceptable and propably compilable. But I thought you wanted to pass a parameter, or didn't you?

              – Constantinius
              May 11 '11 at 17:33














            4












            4








            4







            obviously in your code you are not passing any parameter. To pass a variable you have to do the following (for example):



            #include <iostream> 
            #include <process.h>

            void MyThread(void *data)
            {
            int x = static_cast<int*>(data);
            std::cout << "Hello World! " << x;
            }

            int main()
            {
            int x = 10;
            _beginthreadex(NULL, 0, MyThread, &x, 0, NULL);
            while(true);
            }


            UPDATE: Since you posted the compilation problem later:
            apparently your thread function needs to return an integer:



            int MyThread(void *data)
            {
            std::cout << "Hello World!";
            return 0;
            }





            share|improve this answer















            obviously in your code you are not passing any parameter. To pass a variable you have to do the following (for example):



            #include <iostream> 
            #include <process.h>

            void MyThread(void *data)
            {
            int x = static_cast<int*>(data);
            std::cout << "Hello World! " << x;
            }

            int main()
            {
            int x = 10;
            _beginthreadex(NULL, 0, MyThread, &x, 0, NULL);
            while(true);
            }


            UPDATE: Since you posted the compilation problem later:
            apparently your thread function needs to return an integer:



            int MyThread(void *data)
            {
            std::cout << "Hello World!";
            return 0;
            }






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited May 11 '11 at 17:36

























            answered May 11 '11 at 17:20









            ConstantiniusConstantinius

            24k65279




            24k65279













            • But no variable needs to be passed since MyThread accepts no arguments?

              – Eilidh
              May 11 '11 at 17:20











            • It worked with NULL as the arguments list in _beginthread.

              – Eilidh
              May 11 '11 at 17:21











            • A it does have an argument list a void* and B yes because NULL is a valid void * value.

              – rerun
              May 11 '11 at 17:23













            • So NULL should be acceptable?

              – Eilidh
              May 11 '11 at 17:24











            • of course, the code you wrote is acceptable and propably compilable. But I thought you wanted to pass a parameter, or didn't you?

              – Constantinius
              May 11 '11 at 17:33



















            • But no variable needs to be passed since MyThread accepts no arguments?

              – Eilidh
              May 11 '11 at 17:20











            • It worked with NULL as the arguments list in _beginthread.

              – Eilidh
              May 11 '11 at 17:21











            • A it does have an argument list a void* and B yes because NULL is a valid void * value.

              – rerun
              May 11 '11 at 17:23













            • So NULL should be acceptable?

              – Eilidh
              May 11 '11 at 17:24











            • of course, the code you wrote is acceptable and propably compilable. But I thought you wanted to pass a parameter, or didn't you?

              – Constantinius
              May 11 '11 at 17:33

















            But no variable needs to be passed since MyThread accepts no arguments?

            – Eilidh
            May 11 '11 at 17:20





            But no variable needs to be passed since MyThread accepts no arguments?

            – Eilidh
            May 11 '11 at 17:20













            It worked with NULL as the arguments list in _beginthread.

            – Eilidh
            May 11 '11 at 17:21





            It worked with NULL as the arguments list in _beginthread.

            – Eilidh
            May 11 '11 at 17:21













            A it does have an argument list a void* and B yes because NULL is a valid void * value.

            – rerun
            May 11 '11 at 17:23







            A it does have an argument list a void* and B yes because NULL is a valid void * value.

            – rerun
            May 11 '11 at 17:23















            So NULL should be acceptable?

            – Eilidh
            May 11 '11 at 17:24





            So NULL should be acceptable?

            – Eilidh
            May 11 '11 at 17:24













            of course, the code you wrote is acceptable and propably compilable. But I thought you wanted to pass a parameter, or didn't you?

            – Constantinius
            May 11 '11 at 17:33





            of course, the code you wrote is acceptable and propably compilable. But I thought you wanted to pass a parameter, or didn't you?

            – Constantinius
            May 11 '11 at 17:33











            1














            You pass your data through the fourth parameter. This passes a pointer to i:



            unsigned __stdcall thread(void *arg)
            {
            int *iptr = (int*)arg;
            ...
            }

            int i;
            _beginthreadex(0, 0, thread, &i, 0, 0);


            Note that the thread function signature I used here is different than what you use: I return an unsigned and use the __stdcall calling convention -- this is the signature _beginthreadex expects.



            Depending on what you're trying to do, the new Concurrency Runtime features in VC++ might be simpler to use than explicitly managing your own threads.



            Edit in response to question edit:



            You can pass any valid void pointer, including NULL. If you do that, you can even leave out the parameter's name since you're not using it:



            unsigned __stdcall thread(void*)
            {
            ...
            }

            _beginthreadex(0, 0, thread, 0, 0, 0);





            share|improve this answer


























            • your function does not return any value. this will most certainly lead to a compiler warning/error.

              – Constantinius
              May 11 '11 at 17:38











            • Yes, I left that out for brevity. You also of course shouldn't try to call _beginthreadex from outside of a function!

              – Cory Nelson
              May 11 '11 at 17:45
















            1














            You pass your data through the fourth parameter. This passes a pointer to i:



            unsigned __stdcall thread(void *arg)
            {
            int *iptr = (int*)arg;
            ...
            }

            int i;
            _beginthreadex(0, 0, thread, &i, 0, 0);


            Note that the thread function signature I used here is different than what you use: I return an unsigned and use the __stdcall calling convention -- this is the signature _beginthreadex expects.



            Depending on what you're trying to do, the new Concurrency Runtime features in VC++ might be simpler to use than explicitly managing your own threads.



            Edit in response to question edit:



            You can pass any valid void pointer, including NULL. If you do that, you can even leave out the parameter's name since you're not using it:



            unsigned __stdcall thread(void*)
            {
            ...
            }

            _beginthreadex(0, 0, thread, 0, 0, 0);





            share|improve this answer


























            • your function does not return any value. this will most certainly lead to a compiler warning/error.

              – Constantinius
              May 11 '11 at 17:38











            • Yes, I left that out for brevity. You also of course shouldn't try to call _beginthreadex from outside of a function!

              – Cory Nelson
              May 11 '11 at 17:45














            1












            1








            1







            You pass your data through the fourth parameter. This passes a pointer to i:



            unsigned __stdcall thread(void *arg)
            {
            int *iptr = (int*)arg;
            ...
            }

            int i;
            _beginthreadex(0, 0, thread, &i, 0, 0);


            Note that the thread function signature I used here is different than what you use: I return an unsigned and use the __stdcall calling convention -- this is the signature _beginthreadex expects.



            Depending on what you're trying to do, the new Concurrency Runtime features in VC++ might be simpler to use than explicitly managing your own threads.



            Edit in response to question edit:



            You can pass any valid void pointer, including NULL. If you do that, you can even leave out the parameter's name since you're not using it:



            unsigned __stdcall thread(void*)
            {
            ...
            }

            _beginthreadex(0, 0, thread, 0, 0, 0);





            share|improve this answer















            You pass your data through the fourth parameter. This passes a pointer to i:



            unsigned __stdcall thread(void *arg)
            {
            int *iptr = (int*)arg;
            ...
            }

            int i;
            _beginthreadex(0, 0, thread, &i, 0, 0);


            Note that the thread function signature I used here is different than what you use: I return an unsigned and use the __stdcall calling convention -- this is the signature _beginthreadex expects.



            Depending on what you're trying to do, the new Concurrency Runtime features in VC++ might be simpler to use than explicitly managing your own threads.



            Edit in response to question edit:



            You can pass any valid void pointer, including NULL. If you do that, you can even leave out the parameter's name since you're not using it:



            unsigned __stdcall thread(void*)
            {
            ...
            }

            _beginthreadex(0, 0, thread, 0, 0, 0);






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited May 11 '11 at 17:36

























            answered May 11 '11 at 17:21









            Cory NelsonCory Nelson

            23.1k25284




            23.1k25284













            • your function does not return any value. this will most certainly lead to a compiler warning/error.

              – Constantinius
              May 11 '11 at 17:38











            • Yes, I left that out for brevity. You also of course shouldn't try to call _beginthreadex from outside of a function!

              – Cory Nelson
              May 11 '11 at 17:45



















            • your function does not return any value. this will most certainly lead to a compiler warning/error.

              – Constantinius
              May 11 '11 at 17:38











            • Yes, I left that out for brevity. You also of course shouldn't try to call _beginthreadex from outside of a function!

              – Cory Nelson
              May 11 '11 at 17:45

















            your function does not return any value. this will most certainly lead to a compiler warning/error.

            – Constantinius
            May 11 '11 at 17:38





            your function does not return any value. this will most certainly lead to a compiler warning/error.

            – Constantinius
            May 11 '11 at 17:38













            Yes, I left that out for brevity. You also of course shouldn't try to call _beginthreadex from outside of a function!

            – Cory Nelson
            May 11 '11 at 17:45





            Yes, I left that out for brevity. You also of course shouldn't try to call _beginthreadex from outside of a function!

            – Cory Nelson
            May 11 '11 at 17:45











            0














            The argument to _beginthreadex must be a function with the __stdcall calling convention. Your function has __cdecl. A simple insertion of __stdcall at the correct spot should solve the problem.



            void __stdcall MyThread(void *data)
            {
            std::cout << "Hello World!";
            }





            share|improve this answer
























            • I added that in, but I'm still getting this error - error C2664: '_beginthreadex' : cannot convert parameter 3 from 'void (__stdcall *)(void *)' to 'unsigned int (__stdcall *)(void *)'

              – Eilidh
              May 11 '11 at 17:32
















            0














            The argument to _beginthreadex must be a function with the __stdcall calling convention. Your function has __cdecl. A simple insertion of __stdcall at the correct spot should solve the problem.



            void __stdcall MyThread(void *data)
            {
            std::cout << "Hello World!";
            }





            share|improve this answer
























            • I added that in, but I'm still getting this error - error C2664: '_beginthreadex' : cannot convert parameter 3 from 'void (__stdcall *)(void *)' to 'unsigned int (__stdcall *)(void *)'

              – Eilidh
              May 11 '11 at 17:32














            0












            0








            0







            The argument to _beginthreadex must be a function with the __stdcall calling convention. Your function has __cdecl. A simple insertion of __stdcall at the correct spot should solve the problem.



            void __stdcall MyThread(void *data)
            {
            std::cout << "Hello World!";
            }





            share|improve this answer













            The argument to _beginthreadex must be a function with the __stdcall calling convention. Your function has __cdecl. A simple insertion of __stdcall at the correct spot should solve the problem.



            void __stdcall MyThread(void *data)
            {
            std::cout << "Hello World!";
            }






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered May 11 '11 at 17:30









            PuppyPuppy

            123k25197410




            123k25197410













            • I added that in, but I'm still getting this error - error C2664: '_beginthreadex' : cannot convert parameter 3 from 'void (__stdcall *)(void *)' to 'unsigned int (__stdcall *)(void *)'

              – Eilidh
              May 11 '11 at 17:32



















            • I added that in, but I'm still getting this error - error C2664: '_beginthreadex' : cannot convert parameter 3 from 'void (__stdcall *)(void *)' to 'unsigned int (__stdcall *)(void *)'

              – Eilidh
              May 11 '11 at 17:32

















            I added that in, but I'm still getting this error - error C2664: '_beginthreadex' : cannot convert parameter 3 from 'void (__stdcall *)(void *)' to 'unsigned int (__stdcall *)(void *)'

            – Eilidh
            May 11 '11 at 17:32





            I added that in, but I'm still getting this error - error C2664: '_beginthreadex' : cannot convert parameter 3 from 'void (__stdcall *)(void *)' to 'unsigned int (__stdcall *)(void *)'

            – Eilidh
            May 11 '11 at 17:32











            0














            The fourth parameter in _beginthreadex(NULL, 0, MyThread, NULL, 0, NULL) are arguments to the function MyThread



            Right now you are passing NULL to it so void* data must be getting a NULL pointer



            struct X  
            {
            int a;
            int b;
            };

            X* x = (X*)malloc(sizeof(X));

            x->a = 5;
            x->b = 6;
            _beginthreadex(NULL, 0, MyThread, NULL, x, NULL);


            The above code will pass pointer x to the function MyThread().
            Do take care of X, as it better should be allocated by malloc or new. Do not create it on stack until you use a thread join






            share|improve this answer


























            • NULL is a valid void* type isn't it?

              – atoMerz
              May 11 '11 at 17:29











            • @Atomerz: yeah you are right. I misunderstood the question and did not see the EDIT part of the question

              – Mayank
              May 11 '11 at 17:59
















            0














            The fourth parameter in _beginthreadex(NULL, 0, MyThread, NULL, 0, NULL) are arguments to the function MyThread



            Right now you are passing NULL to it so void* data must be getting a NULL pointer



            struct X  
            {
            int a;
            int b;
            };

            X* x = (X*)malloc(sizeof(X));

            x->a = 5;
            x->b = 6;
            _beginthreadex(NULL, 0, MyThread, NULL, x, NULL);


            The above code will pass pointer x to the function MyThread().
            Do take care of X, as it better should be allocated by malloc or new. Do not create it on stack until you use a thread join






            share|improve this answer


























            • NULL is a valid void* type isn't it?

              – atoMerz
              May 11 '11 at 17:29











            • @Atomerz: yeah you are right. I misunderstood the question and did not see the EDIT part of the question

              – Mayank
              May 11 '11 at 17:59














            0












            0








            0







            The fourth parameter in _beginthreadex(NULL, 0, MyThread, NULL, 0, NULL) are arguments to the function MyThread



            Right now you are passing NULL to it so void* data must be getting a NULL pointer



            struct X  
            {
            int a;
            int b;
            };

            X* x = (X*)malloc(sizeof(X));

            x->a = 5;
            x->b = 6;
            _beginthreadex(NULL, 0, MyThread, NULL, x, NULL);


            The above code will pass pointer x to the function MyThread().
            Do take care of X, as it better should be allocated by malloc or new. Do not create it on stack until you use a thread join






            share|improve this answer















            The fourth parameter in _beginthreadex(NULL, 0, MyThread, NULL, 0, NULL) are arguments to the function MyThread



            Right now you are passing NULL to it so void* data must be getting a NULL pointer



            struct X  
            {
            int a;
            int b;
            };

            X* x = (X*)malloc(sizeof(X));

            x->a = 5;
            x->b = 6;
            _beginthreadex(NULL, 0, MyThread, NULL, x, NULL);


            The above code will pass pointer x to the function MyThread().
            Do take care of X, as it better should be allocated by malloc or new. Do not create it on stack until you use a thread join







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 14 '18 at 13:45









            Richard Chambers

            9,86724067




            9,86724067










            answered May 11 '11 at 17:21









            MayankMayank

            2,40272855




            2,40272855













            • NULL is a valid void* type isn't it?

              – atoMerz
              May 11 '11 at 17:29











            • @Atomerz: yeah you are right. I misunderstood the question and did not see the EDIT part of the question

              – Mayank
              May 11 '11 at 17:59



















            • NULL is a valid void* type isn't it?

              – atoMerz
              May 11 '11 at 17:29











            • @Atomerz: yeah you are right. I misunderstood the question and did not see the EDIT part of the question

              – Mayank
              May 11 '11 at 17:59

















            NULL is a valid void* type isn't it?

            – atoMerz
            May 11 '11 at 17:29





            NULL is a valid void* type isn't it?

            – atoMerz
            May 11 '11 at 17:29













            @Atomerz: yeah you are right. I misunderstood the question and did not see the EDIT part of the question

            – Mayank
            May 11 '11 at 17:59





            @Atomerz: yeah you are right. I misunderstood the question and did not see the EDIT part of the question

            – Mayank
            May 11 '11 at 17:59


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f5968076%2fpassing-parameters-to-beginthreadex%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Xamarin.iOS Cant Deploy on Iphone

            Glorious Revolution

            Dulmage-Mendelsohn matrix decomposition in Python