-
Notifications
You must be signed in to change notification settings - Fork 0
/
hello_world_main_thread_terminated_child_thread_still_alive.c
55 lines (44 loc) · 1.67 KB
/
hello_world_main_thread_terminated_child_thread_still_alive.c
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
/*
* compile using
* gcc -g -c hello_world.c -o hello_world.o
* gcc -g hello_world.o -o hello_world -lpthread
* Run:
* ./hello_world
*/
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h> //for pause() and sleep()
void thread1_create();
void *thread_fn_callback(void * arg);
int main(int argc, char **argv)
{
thread1_create();
printf("Main thread had an advantage over the child threads in the sense that when it completes, all the child threads get terminated too. The viceversa is not available. When the child thread terminates, then it does not affect the main thread at all..\n");
pthread_exit(0); //by terminating the execution of the main thread with pthread_exit(0) , then the child threads maintain their execution independently
return 0;
}
void thread1_create()
{
pthread_t pthread1; //this is a data structure is associated to threads = thread handle
static char *thread_input1="I am the thread number 1."; //This must be static memory or memory on the heap
//Returns 0 on success, otherwise returns an error code, all pthread functions return -ve error code on failure, they do no set a global 'errno' variable
int returned_value=pthread_create(&pthread1,NULL,thread_fn_callback, (void *) thread_input1);
if(returned_value!=0)
{
printf("Error encountered when creating a thread. The error number is %d.\n",returned_value);
exit(0);
}
}
void *thread_fn_callback(void * arg)
{
char *input=(char *) arg;
int i=0;
while(i<10)
{
printf("The input string is: %s.\n", input);
sleep(1);
i++;
}
//The thread terminates after it returns, so to say it executes completely after a finite number of steps
}