+ /* Initialize ring buffer. */
+ ringbuffer_read = 0;
+ ringbuffer_write = 0;
+ ringbuffer_full = sem_init(0);
+ ringbuffer_free = sem_init(RINGBUFFER_SIZE);
+ ringbuffer_lock = sem_init(1);
+ if (NULL == ringbuffer_full
+ || NULL == ringbuffer_free
+ || NULL == ringbuffer_lock) {
+ perror("sem_init()");
+ return EXIT_FAILURE;
+ }
+
+ /* Spawn worker threads to handle requests. */
+ threads = (pthread_t *)malloc(thread_count * sizeof(pthread_t));
+ if (NULL == threads) {
+ perror("thread malloc failed");
+ return EXIT_FAILURE;
+ }
+ for (i = 0; i < thread_count; i++) {
+ int result;
+ pthread_t thread;
+
+ result = pthread_create(&thread, NULL,
+ (void * (*)(void *))&worker_thread,
+ NULL);
+ if (0 != result) {
+ printf("failed to create worker thread: %s\n", strerror(result));
+ return EXIT_FAILURE;
+ }
+
+ threads[i] = thread;
+ }
+