-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
44 lines (36 loc) · 814 Bytes
/
Copy pathmain.c
File metadata and controls
44 lines (36 loc) · 814 Bytes
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
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <memory.h>
#include <syscall.h>
#include <stdint.h>
#include <stdio.h>
#include <unistd.h>
#include <ucontext.h>
#include <dlfcn.h>
#include <time.h>
#include "single-pthread.h"
#define THREAD_COUNT 3
void* print_thread(void* arg)
{
size_t num = (size_t)arg;
size_t run_count = 0;
while (run_count < 3)
{
printf("running thread %zu for the %zu time\n", num, run_count);
run_count++;
sleep(1);
}
return NULL;
}
int main()
{
pthread_t threads[THREAD_COUNT];
for (size_t i = 0;i < THREAD_COUNT; i++)
{
pthread_create(&threads[i], NULL, print_thread, (void*)i + 1);
}
puts("main thread sleeping");
sleep(10); // Should run 1-THREAD_COUNT
puts("main thread returned");
}