/* * Consumer thread main. */ void* consumer_main(void* arg) { consumer_t* consumer = (consumer_t*) arg; workq_t* workq = consumer->workq; for (char* t = (char*) workq_get(workq); t != NULL; t = (char*) workq_get(workq)) { lprintf("Process %d: %s\n", consumer->id, t); free(t); } return NULL; }
int main(void) { workq_msg_t msg; int x; int size = 0; work_queue = workq_init(NULL, 0); if(work_queue < 0) { printf("Failed to initialize a private work queue: error %s (%d)\n", strerror(errno), errno); exit(EXIT_FAILURE); } atexit(kill_q); printf("Adding 10 items to the queue in order...\n"); ADD_OR_DIE(one, work_queue, 1); ADD_OR_DIE(two, work_queue, 2); ADD_OR_DIE(three, work_queue, 3); ADD_OR_DIE(four, work_queue, 4); ADD_OR_DIE(five, work_queue, 5); ADD_OR_DIE(six, work_queue, 6); ADD_OR_DIE(seven, work_queue, 7); ADD_OR_DIE(eight, work_queue, 8); ADD_OR_DIE(nine, work_queue, 9); ADD_OR_DIE(ten, work_queue, 10); printf("Removing from queue...\n"); for(x = 1; x <= 10; ++x) { size = workq_get(work_queue, &msg); if(size < 0) { printf("Error pulling from queue: %s (%d)\n", strerror(errno), errno); exit(EXIT_FAILURE); } else if(size == 0) { printf("No messages left...\n"); } else { printf("Got message \"%s\" priority %ld\n", msg.data, msg.type); } } printf("Adding 10 items to the queue in reverse order...\n"); ADD_OR_DIE(ten, work_queue, 10); ADD_OR_DIE(nine, work_queue, 9); ADD_OR_DIE(eight, work_queue, 8); ADD_OR_DIE(seven, work_queue, 7); ADD_OR_DIE(six, work_queue, 6); ADD_OR_DIE(five, work_queue, 5); ADD_OR_DIE(four, work_queue, 4); ADD_OR_DIE(three, work_queue, 3); ADD_OR_DIE(two, work_queue, 2); ADD_OR_DIE(one, work_queue, 1); printf("Removing from queue...\n"); for(x = 1; x <= 10; ++x) { size = workq_get(work_queue, &msg); if(size < 0) { printf("Error pulling from queue: %s (%d)\n", strerror(errno), errno); exit(EXIT_FAILURE); } else if(size == 0) { printf("No messages left...\n"); } else { printf("Got message \"%s\" priority %ld\n", msg.data, msg.type); } } printf("Tests passed.\n"); exit(EXIT_SUCCESS); }