_beginthreadex static member function

前端 未结 5 1158
名媛妹妹
名媛妹妹 2021-01-24 02:28

How do I create a thread routine of a static member function

class Blah
{
    static void WINAPI Start();
};

// .. 
// ...
// ....

hThread = (HANDLE)_beginthre         


        
5条回答
  •  囚心锁ツ
    2021-01-24 03:23

    class Blah
    {
        static unsigned int __stdcall Start(void*); // void* should be here, because _beginthreadex requires it.
    };
    

    The routine passed to _beginthreadex must use the __stdcall calling convention and must return a thread exit code.

    Implementation of Blah::Start:

    unsigned int __stdcall Blah::Start(void*)
    {
      // ... some code
    
      return 0; // some exit code. 0 will be OK.
    }
    

    Later in your code you could write any of the following:

    hThread = (HANDLE)_beginthreadex(NULL, 0, CBlah::Start, NULL, NULL, NULL);
    // or
    hThread = (HANDLE)_beginthreadex(NULL, 0, &CBlah::Start, NULL, NULL, NULL);
    

    In first case Function-to-pointer conversion will be applied according to C++ Standard 4.3/1. In second case you'll pass pointer to function implicitly.

提交回复
热议问题