aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWladimir J. van der Laan <laanwj@gmail.com>2012-08-28 18:26:35 +0200
committerWladimir J. van der Laan <laanwj@gmail.com>2012-08-29 20:02:07 +0200
commit61d85071405b99c3734606eed31ea8f615c0c77a (patch)
tree6dfa3d0372bc7cb4751da7a70d2fd674a39ca315
parent74d36d44f2584056e2fb7c0b82dc0507e2f0c5e1 (diff)
implement CreateThread with boost::thread
I'm not sure why this wasn't done before. - Removes typedef of pthread_t on Windows, which fixes a native compile issue on mingw.
-rw-r--r--src/util.cpp12
-rw-r--r--src/util.h53
2 files changed, 13 insertions, 52 deletions
diff --git a/src/util.cpp b/src/util.cpp
index 461f42d177..ec24ee4032 100644
--- a/src/util.cpp
+++ b/src/util.cpp
@@ -1299,3 +1299,15 @@ void RenameThread(const char* name)
(void)name;
#endif
}
+
+bool CreateThread(void(*pfn)(void*), void* parg)
+{
+ try
+ {
+ boost::thread(pfn, parg); // thread detaches when out of scope
+ } catch(boost::thread_resource_error &e) {
+ printf("Error creating thread: %s\n", e.what());
+ return false;
+ }
+ return true;
+}
diff --git a/src/util.h b/src/util.h
index 709b0e05bd..fffa60216d 100644
--- a/src/util.h
+++ b/src/util.h
@@ -539,65 +539,14 @@ public:
}
};
+bool CreateThread(void(*pfn)(void*), void* parg);
-
-
-
-
-
-
-
-
-// Note: It turns out we might have been able to use boost::thread
-// by using TerminateThread(boost::thread.native_handle(), 0);
#ifdef WIN32
-typedef HANDLE pthread_t;
-
-inline pthread_t CreateThread(void(*pfn)(void*), void* parg, bool fWantHandle=false)
-{
- DWORD nUnused = 0;
- HANDLE hthread =
- CreateThread(
- NULL, // default security
- 0, // inherit stack size from parent
- (LPTHREAD_START_ROUTINE)pfn, // function pointer
- parg, // argument
- 0, // creation option, start immediately
- &nUnused); // thread identifier
- if (hthread == NULL)
- {
- printf("Error: CreateThread() returned %d\n", GetLastError());
- return (pthread_t)0;
- }
- if (!fWantHandle)
- {
- CloseHandle(hthread);
- return (pthread_t)-1;
- }
- return hthread;
-}
-
inline void SetThreadPriority(int nPriority)
{
SetThreadPriority(GetCurrentThread(), nPriority);
}
#else
-inline pthread_t CreateThread(void(*pfn)(void*), void* parg, bool fWantHandle=false)
-{
- pthread_t hthread = 0;
- int ret = pthread_create(&hthread, NULL, (void*(*)(void*))pfn, parg);
- if (ret != 0)
- {
- printf("Error: pthread_create() returned %d\n", ret);
- return (pthread_t)0;
- }
- if (!fWantHandle)
- {
- pthread_detach(hthread);
- return (pthread_t)-1;
- }
- return hthread;
-}
#define THREAD_PRIORITY_LOWEST PRIO_MAX
#define THREAD_PRIORITY_BELOW_NORMAL 2