-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
80 lines (60 loc) · 1.76 KB
/
main.cpp
File metadata and controls
80 lines (60 loc) · 1.76 KB
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
#include "include/common.h"
#include "core/Thread.h"
#include "core/ParkEvent.h"
#include "core/ObjectMonitor.h"
#include "core/ObjectSynchronizer.h"
#include "oops/InstanceOopDesc.h"
#include "core/BasicLock.h"
#include "oops/Klass.h"
Klass* klass = new Klass;
InstanceOopDesc* obj = new InstanceOopDesc(klass);
BasicLock* lock = new BasicLock;
void* thread_do_1(void* arg) {
Thread* Self = static_cast<Thread *>(arg);
// 进入临界区
ObjectSynchronizer::fast_enter(obj, lock, Self);
for (int i = 0; i < 3; i++) {
INFO_PRINT("[%s] %d\n", Self->name(), i);
usleep(100);
}
// 出临界区
ObjectSynchronizer::fast_exit(obj, lock, Self);
return 0;
}
void* thread_do_2(void *arg) {
Thread* Self = static_cast<Thread*>(arg);
// 进入临界区
ObjectSynchronizer::fast_enter(obj, lock, Self);
for (int i = 0; i < 3; i++) {
INFO_PRINT("[%s] %d\n", Self->name(), i);
usleep(100);
}
// 出临界区
ObjectSynchronizer::fast_exit(obj, lock, Self);
return 0;
}
ParkEvent* main_thread_event = new ParkEvent;
void* main_thread_do(void* args) {
Thread* t1 = new Thread(thread_do_1, NULL, "t1");
Thread* t2 = new Thread(thread_do_2, NULL, "t2");
Thread* t3 = new Thread(thread_do_2, NULL, "t3");
Thread* t4 = new Thread(thread_do_2, NULL, "t4");
Thread* t5 = new Thread(thread_do_2, NULL, "t5");
Thread* t6 = new Thread(thread_do_2, NULL, "t6");
Thread* t7 = new Thread(thread_do_2, NULL, "t7");
t1->run();
t2->run();
t3->run();
t4->run();
t5->run();
t6->run();
t7->run();
t1->join();
return 0;
}
int main() {
pthread_t tid;
pthread_create(&tid, NULL, main_thread_do, NULL);
pthread_join(tid, NULL);
return 0;
}