-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththread.cpp
35 lines (29 loc) · 827 Bytes
/
thread.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/*
* thread.cpp
*
* Created on: Apr 28, 2011
* Author: xiaoguangye
*/
#include "thread.hpp"
namespace base {
static void* threadFunction(void* arg) {
ThreadBody* c = reinterpret_cast<ThreadBody*>(arg);
(*c)();
delete c;
return 0;
}
pthread_t makeThread(ThreadBody body) {
// 'makeThread' may return before the thread it is creating is
// actually scheduled. Therefore we pass a heap allocated copy of
// the thread body down to 'threadFuction' instead of using 'body'
// itself. Ownership of the copy is transferred if the thread is
// successfully created.
ThreadBody* copy = new ThreadBody(body);
void* arg = reinterpret_cast<void*>(copy);
pthread_t thread;
if (pthread_create(&thread, NULL, threadFunction, arg) != 0) {
delete copy;
}
return thread;
}
} // namespace base