Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdio.h>
- #include <windows.h>
- using namespace std;
- const int MAX = 10000;
- struct prArray
- {
- int pStart, pEnd, pTest, pCount;
- int tArray[MAX];
- };
- int check_prime(int);
- DWORD WINAPI primer(LPVOID lpParam);
- int main()
- {
- int one, two, diff, temp, end1, start2;
- cout << " START:\n";
- cin >> one;
- cin >> two;
- prArray ash;
- prArray jeff;
- diff = (two - one);
- temp = diff / 2;
- end1 = one + temp;
- start2 = end1 + 1;
- ash.pStart = one;
- ash.pEnd = one + temp;
- ash.pCount = ash.pStart;
- jeff.pStart = start2;
- jeff.pEnd = two;
- jeff.pCount = jeff.pStart;
- DWORD myThreadID1;
- HANDLE myHandle = CreateThread(
- NULL,
- 0,
- primer,
- &ash,
- 0,
- &myThreadID1);
- DWORD myThreadID2;
- HANDLE myHandle2 = CreateThread(
- 0,
- 0,
- primer,
- &jeff,
- 0,
- &myThreadID2);
- CloseHandle(myHandle);
- CloseHandle(myHandle2);
- cout << endl << endl;
- cout << " BACK TO main()\n";
- cout << "\n THREAD1: \n";
- cout << "Thread1 starting point: " << ash.pStart << endl;
- cout << "Thread1 ending point: " << ash.pEnd << endl;
- cout << "Test: " << ash.pTest << endl;
- int i1;
- for (i1 = 0; i1 < MAX; i1++)
- {
- if (ash.tArray[i1] == 0)
- {
- break;
- }
- else
- {
- cout << ash.tArray[i1] << " ";
- }
- }
- //begin second thread (jeff)
- cout << endl << endl;
- cout << " THREAD2: " << endl;
- cout << "Thread2 starting point: " << jeff.pStart << endl;
- cout << "Thread2 ending point: " << jeff.pEnd << endl;
- cout << "Test: " << jeff.pTest << endl;
- int js;
- for (js = 0; js < MAX; js++)
- {
- if (jeff.tArray[js] == 0)
- {
- break;
- }
- else
- {
- cout << jeff.tArray[js] << " ";
- }
- }
- cout << "\n first = " << i1 << endl;
- cout << " second = " << js << endl;
- cout << "Thread 1 counter = " << ash.pCount << endl;
- cout << "Thread 2 counter = " << jeff.pCount << endl;
- cout << "\n END PROGRAM" << endl;
- system("pause");
- return 0;
- }
- int check_prime(int a)
- {
- int c;
- if ( a == 2 || a == 3 || a == 5 || a ==7) //check if the number is 2
- {
- return 1;
- }
- else if (a % 2 == 0 || a % 3 == 0 || a % 5 == 0 || a % 7 == 0)
- {
- return 0;
- }
- else
- {
- for (c = 11; c <= a - 1; c+=2)
- {
- if (a%c == 0)
- return 0;
- }
- if ( c == a)
- return 1;
- }
- }
- DWORD WINAPI primer(LPVOID lpParam)
- {
- prArray& dan = (prArray)lpParam;
- dan.pTest = (dan.pEnd - dan.pStart);
- for (int i = 0; i < MAX; i++)
- {
- dan.tArray[i] = 0;
- }
- int b = 0;
- for (int lp = dan.pStart; lp <= dan.pEnd; lp++, dan.pCount++)
- {
- int result3 = check_prime(lp);
- if (result3 == 1)
- {
- dan.tArray[b] = lp;
- b++;
- }
- }
- }
Add Comment
Please, Sign In to add comment