0

I had declared a function and passed as 3 parameter of CreateThread() function but I tried to put this function and related in a class apart, to make code more organizated but I got the following error:

error: cannot convert 'long unsigned int (MainWindow::)(void)' to 'LPTHREAD_START_ROUTINE {aka long unsigned int (attribute((stdcall)) )(void)}' for argument '3' to 'void* CreateThread(LPSECURITY_ATTRIBUTES, SIZE_T, LPTHREAD_START_ROUTINE, LPVOID, DWORD, LPDWORD)'

                                                               ^

I guess it's because every first parameter of a method as hidden parameter auto-generated by the compiler which is where this pointer gets passed making the function signature mismatch, right?

Is there any way I can make this work?

It does happens in the call from inside a method:

   hThread = CreateThread( NULL, 0, &func, NULL, 0, NULL);
Jack
  • 16,276
  • 55
  • 159
  • 284
  • By "method" do you mean a class member function? Then it needs to be marked as `static` or it won't work. – Some programmer dude Sep 19 '15 at 19:47
  • 1
    possible duplicate of [run threads of class member function in c++](http://stackoverflow.com/questions/4666635/run-threads-of-class-member-function-in-c) – Captain Obvlious Sep 19 '15 at 19:57
  • @JoachimPileborg: Yes, I mean a member functiono of a class. The problem with `static` is accessing class's properties. On the link pointed out by @captain obvilious seems to have a good design by Nawaz. I'll try it – Jack Sep 19 '15 at 20:16
  • 1
    Since you're using Qt you'd probably be better off with `QThread`. If you don't want an event loop in your thread, you could use `std::thread`. Either way, there's very little reason to use Win32 threads directly in modern C++ development. – MrEricSir Sep 19 '15 at 20:29

1 Answers1

2

Create a function which takes a void*. Use this as parameter and cast it back in function

mksteve
  • 12,614
  • 3
  • 28
  • 50