Added example chapter 7 on Threads
This commit is contained in:
30
chp7/thread.c
Normal file
30
chp7/thread.c
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user