-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththread.cc
32 lines (26 loc) · 806 Bytes
/
thread.cc
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
#include "thread.h"
#include <cstdlib>
#include <cstdio>
#include <errno.h>
pthread_t startDetachedThread( void * (*functor)(void *), void * arg ){
pthread_attr_t DetachedAttr;
pthread_attr_init(&DetachedAttr);
pthread_attr_setdetachstate(&DetachedAttr, PTHREAD_CREATE_DETACHED);
pthread_t handler;
if( pthread_create(&handler, &DetachedAttr, functor, arg) ){
free(arg);
perror("pthread_create");
}
pthread_detach(handler);
// free resources for detached attribute
pthread_attr_destroy(&DetachedAttr);
return handler;
}
pthread_t startThread( void * (*functor)(void *), void * arg ){
pthread_t handler;
if( pthread_create(&handler, NULL, functor, arg) ){
free(arg);
perror("pthread_create");
}
return handler;
}