1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | #define _CRT_SECURE_NO_WARNINGS #include <process.h> #include <Windows.h> #include <tchar.h> #include <iostream> #include <string.h> #include <fstream> using namespace std; int total = 0; #define SIZE 10 #define NumberOfThread 3 DWORD WINAPI ThreadProc_sum(LPVOID pParam); DWORD WINAPI ThreadProc_store(LPVOID pParam); struct ArrayIO { int array[SIZE]; int num; ofstream fout; ifstream fin; CRITICAL_SECTION _sumlock; CRITICAL_SECTION _storelock; ArrayIO() { InitializeCriticalSection(&_sumlock); InitializeCriticalSection(&_storelock); num = 0; memset(array, 0, sizeof(int)*SIZE); } void setArray() { cout << SIZE << "개의 숫자를 입력해주세요" << endl; for (int i = 0; i < SIZE; i++) { cin >> array[i]; } } void printArray() { cout << "array내의 배열원소 값은 : "; for (int i = 0; i < SIZE; i++) { cout << array[i] << ", "; } cout << endl; } }; struct MyThread { HANDLE hThread[NumberOfThread]; DWORD dwThreadId[NumberOfThread]; ~MyThread() { for (int i = 0; i < NumberOfThread; i++) { CloseHandle(hThread[i]); } } void execute(ArrayIO* arr){ arr->num = 0; arr->fin.open("C:\\Test\\text.txt", ifstream::in); for (int i = 0; i < NumberOfThread; i++) { hThread[i] = CreateThread(NULL, 0, ThreadProc_sum, (LPVOID)arr, 0, &dwThreadId[i]); } WaitForMultipleObjects(NumberOfThread, hThread, true, INFINITE); arr->fin.close(); } void store(ArrayIO* arr) { arr->num = 0; arr->fout.open("C:\\Test\\text.txt", ofstream::out); for (int i = 0; i < NumberOfThread; i++) { hThread[i] = CreateThread(NULL, 0, ThreadProc_store, (LPVOID)arr, 0, &dwThreadId[i]); } WaitForMultipleObjects(NumberOfThread, hThread, true, INFINITE); arr->fout.close(); } }; DWORD WINAPI ThreadProc_store(LPVOID pParam) { ArrayIO* t = (ArrayIO*)pParam; int term = SIZE / NumberOfThread; EnterCriticalSection(&t->_storelock); if (t->num != term*(NumberOfThread - 1)) { for (int i = 0; i < term; i++) { t->fout << t->array[t->num]; t->fout.put(' '); t->num = t->num+1; } } else { while (t->num != SIZE) { t->fout << t->array[t->num]; t->fout.put(' '); (t->num)++; } } LeaveCriticalSection(&t->_storelock); return 0; } DWORD WINAPI ThreadProc_sum(LPVOID pParam) { ArrayIO* t = (ArrayIO*)pParam; int term = SIZE / NumberOfThread; EnterCriticalSection(&t->_sumlock); int buf = 0; if (t->num != term*(NumberOfThread - 1)) { for (int i = 0; i < term; i++) { t->fin >> buf; total += buf; (t->num)++; } } else { while (t->num != SIZE) { t->fin >> buf; total += buf; (t->num)++; } } LeaveCriticalSection(&t->_sumlock); return 0; } void printValue(MyThread* t) { cout << "total : " << total << endl; cout << "avg : " << total / NumberOfThread << endl; } int _tmain() { MyThread t; ArrayIO arr1; arr1.setArray(); arr1.printArray(); t.store(&arr1); DeleteCriticalSection(&arr1._storelock); t.execute(&arr1); DeleteCriticalSection(&arr1._sumlock); printValue(&t); return 0; } | cs |
더하는 부분이 지저분 하다. 이때는 왜 아래 처럼 코드를 쓰지 못했을까 궁금하다.
'시스템프로그래밍' 카테고리의 다른 글
이벤트를 사용한 예제 (0) | 2018.05.26 |
---|---|
뮤텍스와 크리티컬섹션을 이용한 기본 예제 (0) | 2018.05.26 |
크리티컬 섹션을 이용한 기본예제.. (0) | 2018.05.26 |
멀티프로세스 기본예제 (0) | 2018.05.26 |
파일 입출력 예제.1 (0) | 2018.05.26 |