반응형




메인 및 3개의 스레드가 함께 움직이는 과정을 나타내는 간단한 코드이다.


arr 배열에 어떤 수가 있고, 스레드1은 앞 3개 스레드2는 그다음 3개 스레드3은 나머지 4개를 정렬한다.(여기서는 선택 정렬을 이용하였다.)


각각의 스레드는 끝나면 check를 +1해주고, 메인 스레드는 각 스레드가 모두 마칠 때 까지 기다린다. (check == 3 일때 까지)


마지막 정렬이 안된 것은 Main에서 병합 정렬로 마무리 하도록 하였다.


코드는 아래와 같다.


의미있는 코드는 아니지만, 스레드(thread)가 무엇인지, 메인과 스레드가 어떻게 서로 움직이게 할 수 있을지 보기 괜찮은 예시인 것 같다.





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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include<iostream>
#include<stdio.h>
#include<Windows.h>
#include<process.h> // 스레드를 위해 존재
#define MAX_SIZE 100
#define swap(a,b) {int tmp = a; a = b; b = tmp;}
 
using namespace std;
 
int merge(int *ar, int len);
 
int arr[MAX_SIZE] = { 9,8,7,6,5,4,3,2,1,}; // Data Segment Array(전역적 배열 선언), Critical Section에 해당
int num; // 파일 입출력에서 받아 낸 수의 개수를 저장하는 변수
int check = 0// check 변수를 통해 스레드들이 작업 끝내는지 확인한다.
 
//첫번째 스레드
unsigned __stdcall Thread_first(void *arg)
{
    int arr_1[MAX_SIZE / 3];
    int cnt = (num / 3* 1;
 
    for (int i = 0; i < cnt; i++)   // 0 ~ num / 3 만큼 가져온다.
    {
        arr_1[i] = arr[i];
    }
 
    for (int i = 0; i < cnt; i++// 선택정렬 이용
    {
        int min = i;
        for (int j = i; j < cnt; j++)
        {
            if (arr_1[min] > arr_1[j]) min = j;
        }
 
        swap(arr_1[i], arr_1[min]);
    }
 
    for (int i = 0; i < cnt; i++)   // 정렬한거 다시 넣기
    {        arr[i] = arr_1[i];        }
 
    check++;
    return 1;
}
 
 
 
//두번째 스레드
unsigned __stdcall Thread_second(void *arg)
{
    int arr_2[MAX_SIZE / 3];
    int cnt = (num / 3* 1;
 
    for (int i = cnt; i < cnt * 2; i++// num / 3 ~ (num / 3)*2 만큼 가져온다
    {
        arr_2[i] = arr[i];
    }
 
    for (int i = cnt; i < cnt * 2; i++// 선택정렬 이용
    {
        int min = i;
        for (int j = i; j < cnt * 2; j++)
        {
            if (arr_2[min] > arr_2[j]) min = j;
        }
 
        swap(arr_2[i], arr_2[min]);
    }
 
 
    for (int i = cnt; i < cnt * 2; i++)  // 정렬한거 다시 넣기
    {        arr[i] = arr_2[i];        }
 
    check++;
    return 1;
}
 
 
//세번째 스레드 
unsigned __stdcall Thread_third(void *arg) 
{
    int arr_3[MAX_SIZE / 3];
    int cnt = (num / 3* 1;
 
    for (int i = cnt * 2; i < num; i++// (num / 3) * 2 ~ num만큼 가져온다
    {
        arr_3[i] = arr[i];
    }
 
 
    for (int i = cnt * 2; i < num; i++// 선택정렬 이용
    {
        int min = i;
        for (int j = i; j < num; j++)
        {
            if (arr_3[min] > arr_3[j]) min = j;
        }
 
        swap(arr_3[i], arr_3[min]);
    }
 
    for (int i = cnt * 2; i < num; i++// 정렬한 것 다시 넣기
    {        arr[i] = arr_3[i];        }
 
    check++;
    return 1;
}
 
 
int main()
{
 
    num = 10
 
    _beginthreadex(NULL, 0, Thread_first, 00, NULL);// first thread start
    _beginthreadex(NULL, 0, Thread_second, 00, NULL);// second thread start
    _beginthreadex(NULL, 0, Thread_third, 00, NULL);// third thread start
 
 
    while (check != 3) {} // 스레드 끝나기까지 기다린다.
 
    while (check == 3// 모든 스레드가 끝나고 main thread에 들어올 때(main 또한 하나의 thread이다.)
    {
        for (int i = 0; i < 10; i++)
            cout << arr[i] << " " << endl;
 
        cout << " 여기까지가 스레드가 정렬한 결과입니다.\n" << "\n 아래부터는 merge가 병합 정렬을 시작합니다." << endl;
 
 
        int len = 10
 
 
        merge(arr, len);
 
        for (int i = 0; i < 10; i++)
            cout << arr[i] << " " << endl;
        return 0;
    }
}
 
 
int merge(int *ar, int len)
{
    if (len < 2return 0;
 
    int mid = len / 2;
    merge(ar, mid);
    merge(ar + mid, len - mid);
 
    int *buf = new int[len];
 
    int i = 0;
    int j = mid;
    int k = 0;
 
    while (i < mid && j < len)
        buf[k++= (ar[i] < ar[j]) ? ar[i++] : ar[j++];
 
    while (i < mid)
        buf[k++= ar[i++];
 
    while (j < len)
        buf[k++= ar[j++];
 
    for (i = 0; i < len; i++)
        ar[i] = buf[i];
 
    delete[] buf;
}
Crocus


반응형

'Basic > C++' 카테고리의 다른 글

cin, cout의 이용 방법 및 견해  (0) 2016.11.08
stack 표준 템플릿 라이브러리 이용 방법  (0) 2016.08.14
연산자 오버로딩  (0) 2016.03.27
생성자의 필요성 및 예제(2)  (0) 2015.11.24
생성자의 필요성 및 예제(1)  (0) 2015.11.24