Skip to content

Instantly share code, notes, and snippets.

@nebkor
Forked from George3d6/thread_c.c
Last active December 13, 2018 22:04
Show Gist options
  • Save nebkor/34494a3fdd68fd23d959fa72bd219dbe to your computer and use it in GitHub Desktop.
Save nebkor/34494a3fdd68fd23d959fa72bd219dbe to your computer and use it in GitHub Desktop.
#include <pthread.h>
#include <unistd.h>
void *work(void *arg) {
printf("Sleeping for %d seconds.\n", *(int *) arg);
sleep(*(uint *) arg);
printf("%d\n", *(int*) arg);
return arg;
}
int main() {
pthread_t t1, t2;
int magic_nr_1 = 46;
int magic_nr_2 = 3;
pthread_create(&t1, NULL, work, (void *) &magic_nr_1);
pthread_create(&t2, NULL, work, (void *) &magic_nr_2);
pthread_join(t1,NULL);
pthread_join(t2,NULL);
}
@nebkor
Copy link
Author

nebkor commented Dec 13, 2018

This has the correct name for the thread worker function, as well as the correct casting for the arguments to the work function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment