Last active
September 2, 2021 09:21
-
-
Save heytulsiprasad/648539d04f757806ed3d22b1a3d10d40 to your computer and use it in GitHub Desktop.
Pthread examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> //Header file for sleep(). man 3 sleep for details. | |
#include <pthread.h> | |
// A normal C function that is executed as a thread | |
// when its name is specified in pthread_create() | |
void *myThreadFun(void *vargp) | |
{ | |
sleep(4); | |
printf("Printing GeeksQuiz from Thread \n"); | |
return NULL; | |
} | |
int main() | |
{ | |
pthread_t thread_id; | |
printf("Before Thread\n"); | |
// Takes 4 arguments | |
// pointer to thread_id, specfic attributes, thread to be used, pass arg to thread | |
pthread_create(&thread_id, NULL, myThreadFun, NULL); | |
// it waits for thread_id to terminate | |
pthread_join(thread_id, NULL); | |
printf("After Thread\n"); | |
exit(0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment