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
| #include "stdafx.h"
#include <Windows.h>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
static vector<int>::iterator it1;
static vector<int>::iterator it2;
template <typename T>
T average(T it1, T it2, T it3)
{
if (*it1 <= *it2)
{
if (*it3 <= *it1)
return it1;
if (*it3 >= *it2)
return it2;
return it3;
}
if (*it3 <= *it2)
return it2;
if (*it3 >= *it1)
return it1;
return it3;
}
template <typename T>
struct para
{
T begin,end;
int num;
para (T b, T e, int n)
{
begin = b;
end = e;
num = n;
}
};
typedef para<vector<int>::iterator> PARAMS;
DWORD WINAPI func1(PVOID param)
{
PARAMS* p = static_cast< PARAMS * > (param);
int a = p->num;
while(p->begin != p->end)
{
if(*p->begin >= a)
{
it1 = p->begin;
//создать 1-ое событие
//ждать 3 событие
}
++p->begin;
}
return 0;
}
DWORD WINAPI func2(PVOID param)
{
PARAMS* p = static_cast< PARAMS * > (param);
int a = p->num;
while(p->begin != p->end)
{
if(*p->begin <= a)
{
it1 = p->begin;
//создать 2-ое событие
//ждать 3 событие
}
}
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
vector<int> b;
for(int i = 0; i< 10; ++i)
b.push_back(rand());
/*for(unsigned int i = 0; i<b.size(); ++i)
cout<<b[i]<<" ";
cout<<'\n';*/
auto p_ave = b.begin();
advance(p_ave,b.size()/2);
/*cout<< *p_ave;
cout<<'\n';*/
swap(*p_ave, *average(b.begin(),--b.end(),p_ave));
/*cout<<'\n';
for(unsigned int i = 0; i<b.size(); ++i)
cout<<b[i]<<" ";*/
/*HANDLE potok[2];
potok[0] = CreateThread(NULL, 0, func, &p1, 0, NULL);
potok[1] = CreateThread(NULL, 0, func, &p2, 0, NULL);
WaitForMultipleObjects(2, potok, FALSE, INFINITE);
*/
PARAMS p1(b.begin(),p_ave,*p_ave);
PARAMS p2(p_ave, b.end(), *p_ave);
HANDLE potok[2];
potok[0] = CreateThread(NULL, 0, func1, &p1, 0, NULL);
potok[1] = CreateThread(NULL, 0, func2, &p2, 0, NULL);
//ждать 1 и 2 события
swap(*it1, *it2);
//инициализировать 3-е событие
//WaitForMultipleObjects(2, potok, FALSE, INFINITE);
return 0;
} |