-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThread.c
81 lines (61 loc) · 1.74 KB
/
Thread.c
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include "Thread.h"
#include <errno.h>
#include <stdlib.h>
// This is provide simply as a sample of how to call the various
// pthread functions that you will need to use to implement multi-threading
// You can use the functions in here or you can just use the pthread routines
// directly.
// When using this collection of functions the -pthread option needs to be
// include on the line doing the linking.
// For example:
// gcc -pthread main.c Thread.c
//
// the constructor, initializes the elements within the thread class
void *createThread(void* (*func)(void*), void * parm) {
struct Thread *thread;
thread = malloc(sizeof(struct Thread));
if (thread) {
thread->entry_pt = func;
thread->arg = parm;
}
return thread;
}
// some essential functions
// create the thread and run it
int runThread(void *vthread, pthread_attr_t *attr)
{
struct Thread *thread = vthread;
if (vthread) {
return pthread_create(&thread->id, attr, thread->entry_pt, thread->arg );
}
return -10;
}
// Terminate this thread
int cancelThread(void * vthread)
{
struct Thread *thread = vthread;
return pthread_cancel(thread->id);
}
int joinThread(void * vthread, void **thread_return)
{
struct Thread *thread = vthread;
return pthread_join(thread->id, thread_return);
}
int detachThread(void *vthread) // reaps a thread
{
struct Thread *thread = vthread;
return pthread_detach(thread->id);
}
// some accessors
// get the ThreadID
pthread_t getThreadID(void *vthread)
{
struct Thread *thread = vthread;
return thread->id;
}
// get the arguments passed to the process entry_point
void* getThreadArg(void * vthread)
{
struct Thread *thread = vthread;
return thread->arg;
}