Skip to content

Instantly share code, notes, and snippets.

@zkylab
Last active February 6, 2021 18:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zkylab/b1604c7c763518cda8c056fa4c72ccd7 to your computer and use it in GitHub Desktop.
Save zkylab/b1604c7c763518cda8c056fa4c72ccd7 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#define NUM_OF_THREAD 10
static int res = 0;
int checkIsItPrime(int n) {
int i, flag = 0;
for (i = 2; i <= n / 2; ++i) {
if (n % i == 0) {
flag = 1;
break;
}
}
if (flag == 0)
return 1;
else
return 0;
}
void *generateNumbersAndCountPrimes(void *arg) {
int k,i;
int primeCounter = 0;
for (i = 0; i < 1000; i++) {
int num = (rand() % (10000 - 1000 + 1)) + 1000;
if (checkIsItPrime(num) != 1)
continue;
else
primeCounter++;
}
res = primeCounter;
}
int main() {
int threadsNumbersStructure[1][1000];
int k, i;
int arr = 1000;
srand(time(NULL));
for (i = 0; i < NUM_OF_THREAD; i++) {
pthread_t thread_id;
pthread_create(&thread_id, NULL, generateNumbersAndCountPrimes, (void *) arr );
pthread_join(thread_id, NULL);
printf("Thread %ld - ", i);
printf("%d primes.\n", res);
}
exit(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment