-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy paththreads_local.cpp
More file actions
61 lines (52 loc) · 1.34 KB
/
Copy paththreads_local.cpp
File metadata and controls
61 lines (52 loc) · 1.34 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 <pthread.h>
#include <stdio.h>
/**
* This function uses a thread that modifies a value on the stack
* of the main thread, hence will only work when all threads have access to the
* same shared memory.
*/
struct threadArgs
{
int idx;
int* ptr;
};
void* threadFunc(void* arg)
{
// Write to the struct passed in
auto args = (threadArgs*)arg;
*args->ptr = 200 + args->idx;
return nullptr;
}
int main()
{
// Spawn a couple of threads
int nThreads = 4;
int x[nThreads];
pthread_t threads[nThreads];
struct threadArgs args[nThreads];
for (int i = 0; i < nThreads; i++) {
args[i].idx = i;
args[i].ptr = &x[i];
int ret = pthread_create(&threads[i], NULL, threadFunc, &args[i]);
if (ret != 0) {
fprintf(stderr, "Error creating thread (%i)\n", ret);
return 1;
}
}
// Join the threads
for (int i = 0; i < nThreads; i++) {
if (pthread_join(threads[i], nullptr)) {
fprintf(stderr, "Error joining thread\n");
return 1;
}
// Check stack variable
int expected = 200 + i;
if (x[i] != expected) {
printf("Thread invocation failed (expected %i but got %i)\n",
expected,
x[i]);
return 1;
}
}
return 0;
}