C Program
Goal: Use sieve of eratosthenes multithreaded
This function works with one thread but I am having issues with getting it to work multithreaded.
typedef struct BitBlock_s {
uint32_t bits;
pthread_mutex_t mutex;
} BitBlock_t;
static BitBlock_t b[4];
void *
sieveoferatosthenes (void * vid)
{
long tid = (long) vid;
for (uint32_t j = tid; j <= val; j += num_threads) {
for (uint32_t p = 2; p *p <= val; p++) {
pthread_mutex_lock(&b[p/32].mutex);
if (testbit(b, p) != 1) {
for (uint32_t i = p * p; i <= val; i += p) {
setbit(b, i);
}
}
pthread_mutex_unlock(&b[p/32].mutex);
}
}
pthread_exit(EXIT_SUCCESS);
}