-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththread_test.c
More file actions
61 lines (51 loc) · 1.82 KB
/
thread_test.c
File metadata and controls
61 lines (51 loc) · 1.82 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
#include "pal/pal_core.h"
#include "pal/pal_thread.h"
#define THREAD_TIME 1000
#define THREAD_COUNT 4
static void* PAL_CALL worker(void* arg)
{
// palLog is thread safe so there should'nt be any race conditions
Int32 id = (Int32)(IntPtr)arg;
palLog(nullptr, "Thread %d: started", id);
palSleep(THREAD_TIME * id);
palLog(nullptr, "Thread %d: finished", id);
return nullptr;
}
bool threadTest()
{
palLog(nullptr, "");
palLog(nullptr, "===========================================");
palLog(nullptr, "Thread Test");
palLog(nullptr, "===========================================");
palLog(nullptr, "");
PalResult result;
PalThread* threads[THREAD_COUNT];
// fill the thread creation struct
PalThreadCreateInfo createInfo = {0};
createInfo.entry = worker; // will be the same for all threads
createInfo.stackSize = 0; // same for all threads
createInfo.allocator = nullptr; // default
for (Int32 i = 0; i < THREAD_COUNT; i++) {
createInfo.arg = (void*)((IntPtr)i + 1);
// create thread
result = palCreateThread(&createInfo, &threads[i]);
if (result != PAL_RESULT_SUCCESS) {
const char* error = palFormatResult(result);
palLog(nullptr, "Failed to create thread: %s", error);
return false;
}
}
// join threads
for (Int32 i = 0; i < THREAD_COUNT; i++) {
// we dont need the return value
// joint threads does not need to be detached
result = palJoinThread(threads[i], nullptr);
if (result != PAL_RESULT_SUCCESS) {
const char* error = palFormatResult(result);
palLog(nullptr, "Failed to join threads: %s", error);
return false;
}
}
palLog(nullptr, "All threads finished successfully");
return true;
}