#include #include #include #include // User defined arguments to thread function. typedef struct { const char* msg; int sleepSecs; } thread_creation_info_t; // Thread main function. void* Child(void* args) { thread_creation_info_t* info = (thread_creation_info_t*) args; size_t i; for (i = 0; i < 100; ++i) { printf(" %s ", info->msg); sleep(info->sleepSecs); } return NULL; } int main(int argc, char* argv[]) { #define SLEEP_SECS 1 pthread_t child_thread1; pthread_t child_thread2; thread_creation_info_t info1 = { "+", SLEEP_SECS }; thread_creation_info_t info2 = { "-", SLEEP_SECS }; if (pthread_create(&child_thread1, NULL, &Child, &info1) != 0) { fprintf(stderr, "Creation of thread1 failed.\n"); return -1; } if (pthread_create(&child_thread2, NULL, &Child, &info2) != 0) { fprintf(stderr, "Creation of thread2 failed.\n"); return -1; } // Wait until both threads have finished. pthread_join(child_thread1, NULL); pthread_join(child_thread2, NULL); return 0; }