problems using CreateThread on a member function

前端 未结 1 1681
后悔当初
后悔当初 2021-01-24 01:49

I am trying to create a thread in an object, however I get an error saying \'&\' : illegal operation on bound member function expression. Reading up I saw I hav

1条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-24 02:26

    static functions aren't bound to a particular instance; there is no this pointer, and you have no "member variables." You can pass the this pointer as an argument to your function, and then cast it into a Dac* and access member variables from it.

    So you could do

    ping_thread = CreateThread (NULL , 0, ping_loop, (LPVOID)this, 0, &dping_thread);
    

    And change your ping_loop to this:

    static DWORD WINAPI ping_loop(void* param)
    {
        Dac* dac = (Dac*)param;
        while ( dac->com.dac_ping() == 0)
            Sleep(900);
    
        return 1; //since this is an infinite loop, if the loop breaks, it has failed
    }
    

    0 讨论(0)
提交回复
热议问题