Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- #include <iostream>
- #define NumThread 9
- #define dokladnosc 9999999999
- using namespace std;
- HANDLE mutex;
- struct Dane
- {
- double pi;
- int i;
- int arg;
- };
- double pi(int n) {
- double sum = 0.0;
- int sign = 1;
- for (int i = 0; i < n; ++i) {
- sum += sign / (2.0*i + 1.0);
- sign *= -1;
- }
- return 4.0*sum;
- }
- DWORD WINAPI watek(LPVOID LpParam) {
- Dane* dane = (Dane*)LpParam;
- WaitForSingleObject(mutex, INFINITE);
- while(dane->i < dokladnosc)
- {
- dane->pi += dane->arg / (2.0*dane->i +1);
- dane->arg *= -1;
- dane->i++;
- }
- ReleaseMutex(mutex);
- cout << "exit" << endl;
- return 0;
- }
- int main()
- {
- DWORD id;
- Dane* dane = new Dane(); HANDLE w[NumThread];
- dane->arg = 1;
- dane->pi = 0;
- dane->i = 0;
- mutex = CreateMutex(NULL, false, NULL);
- cout << pi(dokladnosc)<<endl;
- for (int i = 0; i < NumThread; ++i)
- {
- w[i] = CreateThread(NULL, 0, watek, dane, 0, &id);
- }
- WaitForMultipleObjects(NumThread, w, true, 10000);
- cout << dane->pi*4 << endl;
- for (int i = 0; i < NumThread; ++i)
- {
- CloseHandle(w[i]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement