#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h> // For sleep function
void *print_message_function(void *ptr);
int main()
{
pthread_t thread1, thread2;
char *message1 = "Thread 1";
char *message2 = "Thread 2";
int iret1, iret2;
/* Create independent threads each of which will execute function */
iret1 = pthread_create(&thread1, NULL, print_message_function, (void *)message1);
// Introduce a delay to ensure thread1 has started (not reliable for all cases)
sleep(1); // Sleep for 1 second
iret2 = pthread_create(&thread2, NULL, print_message_function, (void *)message2);
/* Wait till threads are complete before main continues. Unless we */
/* wait we run the risk of executing an exit which will terminate */
/* the process and all threads before the threads have completed. */
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
printf("Thread 1 returns: %d\n", iret1
); printf("Thread 2 returns: %d\n", iret2
);
}
void *print_message_function(void *ptr)
{
char *message;
message = (char *)ptr;
return NULL;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPHB0aHJlYWQuaD4KI2luY2x1ZGUgPHVuaXN0ZC5oPiAvLyBGb3Igc2xlZXAgZnVuY3Rpb24KCnZvaWQgKnByaW50X21lc3NhZ2VfZnVuY3Rpb24odm9pZCAqcHRyKTsKCmludCBtYWluKCkKewogICAgcHRocmVhZF90IHRocmVhZDEsIHRocmVhZDI7CiAgICBjaGFyICptZXNzYWdlMSA9ICJUaHJlYWQgMSI7CiAgICBjaGFyICptZXNzYWdlMiA9ICJUaHJlYWQgMiI7CiAgICBpbnQgaXJldDEsIGlyZXQyOwoKICAgIC8qIENyZWF0ZSBpbmRlcGVuZGVudCB0aHJlYWRzIGVhY2ggb2Ygd2hpY2ggd2lsbCBleGVjdXRlIGZ1bmN0aW9uICovCiAgICBpcmV0MSA9IHB0aHJlYWRfY3JlYXRlKCZ0aHJlYWQxLCBOVUxMLCBwcmludF9tZXNzYWdlX2Z1bmN0aW9uLCAodm9pZCAqKW1lc3NhZ2UxKTsKCiAgICAvLyBJbnRyb2R1Y2UgYSBkZWxhe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