logo

Static initializers in the PTHREAD API

#include <pthread.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <time.h> #define THREAD_NUM 2 pthread_mutex_t mutexFuel = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t condFuel = PTHREAD_COND_INITIALIZER; void* routine(void* args) { } int main(int argc, char *argv[]) { pthread_t th[THREAD_NUM]; int i; for (i = 0; i < THREAD_NUM; i++) { if (pthread_create(&th[i], NULL, &routine, NULL) != 0) { perror("Failed to create thread"); } } for (i = 0; i < THREAD_NUM; i++) { if (pthread_join(th[i], NULL) != 0) { perror("Failed to join thread"); } } return 0; }
Course thumb

The C programming language made simple

Courses with this lesson