-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathThreadCreation.c
More file actions
38 lines (38 loc) · 1.04 KB
/
ThreadCreation.c
File metadata and controls
38 lines (38 loc) · 1.04 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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<pthread.h> //It allows a program to control multiple different flows of work that overlap in time.
void * thread_function(void*arg);
char message[]= "Hello World";
int main()
{
int res;
pthread_t a_thread;
void *thread_result;
res = pthread_create(&a_thread,NULL,thread_function,(void*)message); //NULL constaint , (void*)messaage argument
if (res!=0)
{
perror("\n Error: Thread Cannot be created");
exit(EXIT_FAILURE);
}
printf("Waiting foe tread to finish\n");
res = pthread_join(a_thread,&thread_result);
if (res!=0)
{
perror("Thread Cannot be joind \n");
exit(EXIT_FAILURE);
}
printf("\nThread joind & it returned: %s \n ",(char*)thread_result);
printf("The message in now %s\n",message);
exit(EXIT_FAILURE);
}
void *thread_function(void* arg)
{
printf("thread_function is running. The argument was %s\n",(char*)arg);
sleep(3);
strcpy(message,"BYE!");
pthread_exit("Thank you for the cpu time");
}
//gcc ProgramName -lpthread
//int m = atoi(n)