forked from ihaku4/c-linux
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path35-2-create-thread.c
More file actions
36 lines (31 loc) · 687 Bytes
/
35-2-create-thread.c
File metadata and controls
36 lines (31 loc) · 687 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
#include <pthread.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
pthread_t ntid;
void printids(const char *s)
{
pid_t pid;
pthread_t tid;
pid = getpid();
tid = pthread_self();
printf("%s pid %u tid %u (0x%x)\n", s, (unsigned int) pid, (unsigned int) tid, (unsigned int) tid);
printf("%s child thread id: %u\n",s, (unsigned int) ntid);
}
void *thr_fn(void *arg)
{
printids(arg);
return NULL;
}
int main(void)
{
int err;
if (0 != (err = pthread_create(&ntid, NULL, thr_fn, "new thread: "))) {
fprintf(stderr, "can't create thread: %s\n", strerror(err));
exit(1);
}
printids("main thread:");
sleep(1);
return 0;
}