Browse Source

a portable thread class (#1081)

* a portable thread class

* Update platform.h.in

* Update platform.h.in
tags/20190908
nihui GitHub 7 years ago
parent
commit
5ce6ae90be
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 34 additions and 0 deletions
  1. +34
    -0
      src/platform.h.in

+ 34
- 0
src/platform.h.in View File

@@ -28,6 +28,7 @@
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <process.h>
#else
#include <pthread.h>
#endif
@@ -96,6 +97,39 @@ private:
};
#endif // _WIN32

#if _WIN32
static unsigned __stdcall start_wrapper(void* args);
class Thread
{
public:
Thread(void* (*start)(void*), void* args = 0) { _start = start; _args = args; handle = (HANDLE)_beginthreadex(0, 0, start_wrapper, this, 0, 0); }
~Thread() {}
void join() { WaitForSingleObject(handle, INFINITE); CloseHandle(handle); }
private:
friend static unsigned __stdcall start_wrapper(void* arg);
HANDLE handle;
void* (*_start)(void*);
void* _args;
};

static unsigned __stdcall start_wrapper(void* args)
{
Thread* t = (Thread*)args;
t->_start(t->_args);
return 0;
}
#else // _WIN32
class Thread
{
public:
Thread(void* (*start)(void*), void* args = 0) { pthread_create(&t, 0, start, args); }
~Thread() {}
void join() { pthread_join(t, 0); }
private:
pthread_t t;
};
#endif // _WIN32

} // namespace ncnn

#endif // NCNN_PLATFORM_H

Loading…
Cancel
Save