Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <pthread.h>
- #define THREAD_POOL_SIZE 2
- void *do_work(void *args) {
- sleep(2);
- int value = *((int*)args);
- int sq = value * value;
- printf("%d\n", sq);
- return NULL;
- }
- int main(void) {
- //you need to setup four async operations and wait for them to be done before processing further operations.
- pthread_t thread_pool[THREAD_POOL_SIZE];
- int proc_items[] = { 1, 8, 17, 12, 13, 14, 19, 22, 31, 56, 43, 77 };
- int items_left = THREAD_POOL_SIZE;
- for (int i=0; i<12; ++i) {
- int idx = i % THREAD_POOL_SIZE;
- pthread_create(&thread_pool[idx], NULL, do_work, (void*)&proc_items[i]);
- items_left--;
- if (items_left == 0) {
- void *retval;
- pthread_join(thread_pool[(i - (THREAD_POOL_SIZE - 1)) % THREAD_POOL_SIZE], &retval);
- items_left++;
- }
- }
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement