• API
• FAQ
• Tools
• Archive
SHARE
TWEET # Untitled a guest Apr 18th, 2019 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. #include <cstdio>
2. #include <vector>
3. #include <iostream>
4. #include <algorithm>
6. #include <cmath>
7. #include<stdio.h>
8.
9. // Zrobić 2 zadania z Lab 7 w instrukcji.
10.
11. std::vector<int> * primes = NULL;
12. pthread_mutex_t * mutex = NULL;
13.
14. typedef struct job {
15.     int num;
16. } job;
17.
18. void cache_prime(int prime) {
20.     primes->push_back(prime);
22. }
23.
24. void * worker(void * params) {
25.     int target = ((job *)params)->num;
26.     printf("Calculating: %d \n", target);
27.     //std::cout << "Calculating: " << target << std::endl;
28.
29.     for (int i = 2; i < target; i++) {
30.         if (target % i == 0) {
31.             printf("%d is not prime.\n", target);
32.             //std::cout << target << " is not prime..." << std::endl;
33.             return NULL;
34.         }
35.     }
36.     printf("%d is prime! \n", ((job *)params)->num);
37.     //std::cout << ((job *)params)->num << " is prime!" << std::endl;
38.     cache_prime(((job *)params)->num);
39.     return NULL;
40. }
41.
42. int main() {
43.     int from = 4, to = 100;
44.
46.     primes = new std::vector<int>();
47.
50.     job * jobs = { NULL };
51.
52.     if (from > to) std::swap(from, to);
53.     if (from <= 1) from = 2;
54.     if (to <= 1) to = 2;
55.     printf("Looking for primes between %d and %d (inclusive)...\n", from, to);
56.     //std::cout << "Looking for primes between " << from << " and " << to << " (inclusive)..." << std::endl;
57.
58.     for (int i = from; i <= to; i++) {
59.         int thread_index = (i - from) % 4;
62.
66.         }
67.
68.         job * work = (job *)malloc(sizeof(work));
69.         work->num = i;
70.
74.     }
75.
76.     for (int i = 0; i < 4; i++) {