/*
* Simple hello world using pthreads
* Written by Ori Idan Based on demonstration by Gilad Ben-Yosef of codefidence Ltd.
*
* This simple example shows a thread printing message and posting semaphore when done.
* The main process waits for this semaphore and then creates the second thread.
*/
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
#define pthread_attr_default NULL
sem_t my_sem;
void print_message_function( void *ptr )
{
char *message;
message = (char *) ptr;
printf("%s ", message);
sem_post(&my_sem);
pthread_exit(0);
}
int main(void)
{
pthread_t thread1, thread2;
char *message1 = "Hello";
char *message2 = "World";
sem_init(&my_sem, 0, 0);
pthread_create( &thread1, pthread_attr_default,
(void*)&print_message_function, (void*) message1);
sem_wait(&my_sem);
pthread_create(&thread2, pthread_attr_default,
(void*)&print_message_function, (void*) message2);
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
printf("!\n");
sem_destroy(&my_sem);
return 0;
}