Added example chapter 7 on Threads

This commit is contained in:
Fabio Scotto di Santolo
2025-07-28 16:06:28 +02:00
parent 4c611e06ce
commit da62f6025f
3 changed files with 161 additions and 0 deletions

30
chp7/thread.c Normal file
View File

@@ -0,0 +1,30 @@
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
void *start_thread(void *message)
{
printf("%s\n", (const char *)message);
return message;
}
int main(void)
{
pthread_t thing1, thing2;
const char *message1 = "Thing 1";
const char *message2 = "Thing 2";
/* Create two threads, each with a different message. */
pthread_create(&thing1, NULL, start_thread, (void *)message1);
pthread_create(&thing2, NULL, start_thread, (void *)message2);
/*
* Wait for the threads to exit. If we didn't join here,
* we'd risk terminating this main thread before the
* other two threads finished.
*/
pthread_join(thing1, NULL);
pthread_join(thing2, NULL);
return EXIT_SUCCESS;
}