Question: 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.

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);

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!