// created by ztwaker on 2006-8-16
// Used for:? multithread app.
?
#define
WIN32_LEAN_AND_MEAN
#include
<windows.h>
#include
<
iostream
>
using
namespace
std
;
?
#include
<process.h>
?
const
int
n
???? = 8;???????????
// threads count
const
int
size
? = 1025;????????
// 1048577 = 1024*1024+1;
const
int
psize
= (
size
-1)/
n
;??
// size per thread
char
?
pch
[
size
] = { 0 };???
// share memory
HANDLE
threadpool
[
n
];
?
void
proc(
void
*
p
)
{
???
char
*
px
=
static_cast
<
char
*>(
p
);
???
for
(
int
i
= 0;
i
!=
psize
; ++
i
)
??? {
???????
//*(px+i) = char('0' + rand() % 2);
??????? *(
px
+
i
) =
char
(
'A'
+ (
px
-
pch
)/
psize
);
??? }
???
cout
<<
"done"
<<
endl
;
???
_endthread
();
}
?
void
test()
{
???
for
(
int
i
= 0;
i
!=
n
; ++
i
)
??? {
???????
char
*
pt
=
pch
+
psize
*
i
;
???????
unsigned
long
handle
=
_beginthread
(proc, 0,
static_cast
<
void
*>(
pt
));
???????
if
(
handle
!= -1)
??????? {
???????????
threadpool
[
i
] =
reinterpret_cast
<
HANDLE
>(
handle
);
???????????
CloseHandle
(
threadpool
[
i
]);
?
?????? }
??? }
???
???
WaitForMultipleObjects
(
n
,
threadpool
,
true
,
INFINITE
);
}
?
?
int
main()
{
??? test();
???
Sleep
(0);
???
cout
<<
pch
<<
endl
;
???
return
0;
}