1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <assert.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include "thread_pool.h"
const uint64_t MAX_CANDIDATE = 100000;
const size_t NUM_THREADS_PER_POOL = 8;
const size_t NUM_THREADPOOLS = 10;
void check_prime(void *_n) {
uint64_t n = *(uint64_t *) _n;
free(_n);
for (uint64_t k = n / 2; k > 1; k--) {
if (n % k == 0) {
return;
}
}
printf("%" PRIu64 "\n", n);
}
int main() {
thread_pool_t *pools[NUM_THREADPOOLS];
for (size_t i = 0; i < NUM_THREADPOOLS; i++) {
pools[i] = thread_pool_init(NUM_THREADS_PER_POOL);
}
for (uint64_t i = 2; i < MAX_CANDIDATE; i++) {
uint64_t *aux = malloc(sizeof(uint64_t));
assert(aux != NULL);
*aux = i;
thread_pool_add_work(pools[i % NUM_THREADPOOLS], check_prime, aux);
}
for (size_t i = 0; i < NUM_THREADPOOLS; i++) {
thread_pool_finish(pools[i]);
}
}