0

This is the method i use to start threads and it works, but I wonder are there any downsides to this way.

void myFunc()
{
    //code here

}


unsigned int _stdcall ThreadFunction(void* data)
{
    myFunc();
    return 0;
}

I my main function i use :

HANDLE A = (HANDLE)_beginthredex(0,0,&ThreadFunction,0,0,0);

And I end the thread with CloseHandle(A);.

Charles
  • 1,384
  • 11
  • 18
Nikola
  • 11
  • 4

1 Answers1

8

If you have access to C++11 use the <thread> library and you won't need to worry about cross-platform compatibility:

#include <thread>

std::thread t(&ThreadFunction, nullptr);

To wait for the thread's execution to finish, use join():

t.join();

This blocks until the function that the thread is supposed to run has returned.

Otherwise, use CreateThread (since it looks like you're on Windows) or beginthreadex.

For POSIX, use pthread_create().

Hatted Rooster
  • 35,759
  • 6
  • 62
  • 122
  • On Windows [`_beginthreadex` might have some advantages over `CreateThread`](http://stackoverflow.com/questions/331536/windows-threading-beginthread-vs-beginthreadex-vs-createthread-c), like supporting thread_local variables. – Bo Persson Oct 20 '16 at 13:18