Skip to content

Instantly share code, notes, and snippets.

@heytulsiprasad
Last active September 2, 2021 09:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heytulsiprasad/648539d04f757806ed3d22b1a3d10d40 to your computer and use it in GitHub Desktop.
Save heytulsiprasad/648539d04f757806ed3d22b1a3d10d40 to your computer and use it in GitHub Desktop.
Pthread examples
#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