1. CreateThread
#include <windows.h>
#include <iostream.h>
volatile int n;
DWORD WINAPI Add(LPVOID iNum)
{
cout << "Thread is started." << endl;
n += (int)iNum;
cout << "Thread is finished." << endl; return 0;
}
int main()
{
int inc = 10;
HANDLE hThread;
DWORD IDThread;
cout << "n = " << n << endl;
hThread = CreateThread(NULL, 0, Add, (void*)inc, 0, &IDThread);
if (hThread == NULL)
return GetLastError();
// Add
WaitForSingleObject(hThread, INFINITE);
// Add
CloseHandle(hThread);
cout << "n = " << n << endl;
return 0;
}
, WaitForSingleObject, Add.
. , : main worker.
main :
1. , .
2. worker.
3. . 7 .
4. worker.
5. , , .
6. .
worker :
1. . 12 .
2. .
worker :
DWORD WaitForSingleObject(
HANDLE hHandle,//
DWORD dwMilliseconds //
);
INFINITE.
WaitForSingleObject(hThread, INFINITE); //
hThread worker.
:
VOID Sleep(
DWORD dwMilliseconds //
);
,
Sleep (12); // 12
3. .
1) worker .
2) worker .
3) worker .
4) worker .
5) worker .
6) worker .
7) worker .
8) worker .
9) worker .
10) worker , .
|
|
11) worker , .
12) worker , .
13) worker , .
14) worker .
15) worker .
16) worker .
17) worker .
18) worker .
19) worker .
20) worker .
21) worker .
22) worker .
23) worker .
24) worker .
1. .
2. . ?
2. ?
3. ?
4. .
5. ?
4
:
:
1.
2. , .
3. , .
1. Windows
Windows , , CRITICAL_SECTION, . :
VOID InitializeCriticalSection (LPCRITICAL_SECTION lpCriticalSection);
VOID EnterCriticalSection (LPCRITICAL_SECTION lpCriticalSection);
BOOL TryEnterCriticalSection (LPCRITICAL_SECTION
lpCriticalSection);
VOID LeaveCriticalSection (LPCRITICAL_SECTION lpCriticalSection);
VOID DeleteCriticalSection (LPCRITICAL_SECTION lpCriticalSection);
, CRITICAL_SECTION. , TryEnterCriticalSection, . , TryEnterCriticalSection Windows 2000.
. , , . :
|
|
− CRITICAL_SECTION, ;
− CRITICAL_SECTION InitializeCriticalSection;
− EnterCriticalSection, , , ;
− , , LeaveCriticalSection;
− CRITICAL_SECTION, , . DeleteCriticalSection.
. , , , .
1.
#include <windows.h>
#include <iostream>
using namespace std;
DWORD WINAPI thread(LPVOID)
{
int i,j;
for (j = 0; j < 10; j++)
{
for (i = 0; i < 10; i++)
{
cout << j << ' '; cout << flush;
Sleep(22);
}
cout << endl;
}
return 0;
}
int main()
{
int i,j;
HANDLE hThread;
DWORD IDThread;
hThread=CreateThread(NULL, 0, thread, NULL, 0, &IDThread);
if (hThread == NULL)
return GetLastError();
// ,
// for (j = 10; j < 20; j++)
{
for (i = 0; i < 10; i++)
{
cout << j << ' '; cout << flush;
Sleep(22);
}
cout << endl;
}
// , thread
WaitForSingleObject(hThread, INFINITE);
return 0;
}
main thread . - , . : main thread, . CRITICAL_SECTION.
2.
#include <windows.h>
#include <iostream>
using namespace std;
CRITICAL_SECTION cs;
DWORD WINAPI thread(LPVOID)
{
int ij;
for (j = 0; j < 10; j++)
{
// EnterCriticalSection (&cs);
for (i = 0; i < 10; i++)
{
cout << j << ' '; cout.flush();
}
cout << endl;
//
LeaveCriticalSection(&cs);
}
return 0;
}
int main()
{
int i,j;
HANDLE hThread;
DWORD IDThread;
//
InitializeCriticalSection(&cs);
hThread=CreateThread(NULL, 0, thread, NULL, 0, &IDThread);
if (hThread == NULL)
return GetLastError();
// ,
//
for (j = 10; j < 20; j++)
{
//
EnterCriticalSection(&cs);
for (i = 0; i < 10; i++)
{
cout << j << ' '; cout.flush();
}
cout << endl;
//
LeaveCriticalSection(&cs);
}
//
DeleteCriticalSection(&cs);
// , thread
WaitForSingleObject(hThread, INFINITE);
return 0;
}
TryEnterCriticalSection. EnterCriticalSection TryEnterCriticalSection . , TryEnterCriticalSection Windows 2000.
|
|
3. .
// Windows 2000.
#include <windows.h>
#include <iostream>
using namespace std;
CRITICAL_SECTION cs;
DWORD WINAPI thread(LPVOID)
{
int ij;
for (j = 0; j < 10; j++)
{
//
TryEnterCriticalSection (&cs);
for (i = 0; i < 10; i++)
{
cout << j << " "; cout.flush();
}
cout << endl;
//
LeaveCriticalSection(&cs);
}
return 0;
}
int main()
{
int i, j;
HANDLE hThread;
DWORD IDThread;
//
InitializeCriticalSection(&cs);
hThread=CreateThread(NULL, 0, thread, NULL, 0, &IDThread);
if (hThread == NULL)
return GetLastError();
// ,
//
for (j = 10; j < 20; j++)
{
//
TryEnterCriticalSection(&cs);
for (i = 0; i < 10; i++)
{
cout << j << " "; cout.flush();
}
cout << endl;
//
LeaveCriticalSection(&cs);
}
//
DeleteCriticalSection(&cs);
// thread
WaitForSingleObject(hThread, INFINITE);
return 0;
}
, , CRITICAL_SECTION , , , .
2.
, . , . , , . s - , :
P(s) {
s >0 s = s - 1; //
s; //
}
V(s) {
s, ;
s = s + 1;
}
P V , . , P , 1 , . , , , V . , , . . FIFO, , .
, 0 1, . , , 1, . , . .
semaphor s = 1; //
void thread_1() void thread_2()
{ {
|
|
P(s); P(s);
if (n%2 == 0) n++;
n = a; V(s);
else.
n = b;.
V(s);.
. }
}
, n thread_1 thread_2. , .
, thread_1 n , thread_2 . :
semaphor s = 0; //
void thread_1() void thread_2()
{ {
P(s); n++;
if (n%2 == 0) V(s);
n = a;.
else.
n = b;.
. }
}
, .
3. Windows
Windows Semaphores, , . . CreateSemaphore, :
HANDLE CreateSemaphore(
LPSECURITY_ATTRIBUTES lpSemaphoreAttribute, //
LONG lInitialCount, //
LONG lMaximumCount, //
LPCTSTR lpName //
);
, lpSemaphoreAttributes NULL. . lInitialCount , 0 , lMaximumCount.
CreateSemaphore , - NULL. , CreateSemaphore , GetLastError, CreateSemaphore ERROR_ALREADY_EXISTS.
1 . ReleaseSemaphore, :
BOOL Release Semaphore(
HANDLE hSemaphore, //
LONG lRelease Count, // ,
// LPLONG lpPreviousCount //
);
ReleaseSemaphore TRUE, - FALSE. lReleaseCount
, ReleaseSemaphore FALSE .
lpPreviousCount NULL. .
, . .
4.
#include <windows.h>
#include <iostream>
using namespace std;
volatile int a[10];
DWORD WINAPI thread(LPVOID)
{
int i;
for (i = 0; i < 10; i++)
{
a[i] = i + 1;
Sleep(17);
}
return 0;
}
int main()
{
int i;
HANDLE hThread;
DWORD IDThread;
cout << "An initial state of the array: ";
for (i = 0; i < 10; i++)
cout << a[i] <<' ';
cout << endl;
// ,
hThread = CreateThread(NULL, 0, thread, NULL, 0, &IDThread);
if (hThread == NULL)
return GetLastError();
// main
cout << "A modified state of the array: ";
for (i = 0; i < 10; i++)
{
cout << a[i] << ' '; cout.flush();
Sleep(17);
}
cout << endl;
CloseHandle(hThread);
return 0;
}
. thread a , .
main . thread main , , main. , main a thread. . , .
5.
#include <windows.h>
|
|
#include <iostream>
using namespace std;
volatile int a[10];
HANDLE hSemaphore;
DWORD WINAPI thread(LPVOID)
{
int i;
for (i = 0; i < 10; i++)
{
a[i] = i + 1;
// , ReleaseSemaphore(hSemaphore, 1,NULL);
Sleep(500);
}
return 0;
}
int main()
{
int i;
HANDLE hThread;
DWORD IDThread;
cout << "An initial state of the array: ";
for (i = 0; i < 10; i++)
cout << a[i] <<' '; cout << endl;
//
hSemaphore=CreateSemaphore(NULL, 0, 10, NULL); if (hSemaphore == NULL)
return GetLastErrorO;
// , hThread = CreateThread(NULL, 0, thread, NULL, 0, &IDThread); if (hThread == NULL)
return GetLastError();
// main // thread cout << "A final state of the array: "; for (i = 0; i < 10; i++)
{
WaitForSingleObject(hSemaphore, INFINITE);
cout << a[i] << ' ';
cout.flush();
}
cout << endl;
CloseHandle(hSemaphore);
CloseHandle(hThread);
return 0;
}
: 10. , . , . , , , . , . , . , thread, . main , . , . , , , , - . , - -, , , . .
CreateSemaphore OpenSemaphore. CreateSemaphore, lInitialCount lMaximalCount , , , , , lpName. OpenSemaphore, , , . :
HANDLE OpenSemaphore(
DWORD dwDesiredAccess, //
BOOL bInheritHandle, //
LPCTSTR lpName //
);
dwDesiredAccess , :
SEMAPHORE_ALL_ACCESS
SEMAPHORE_MODIFY_STATE
SYNCHRONIZE
SEMAPHORE_ALL_ACCESS . , . SEMAPHORE_MODIFY_STATE , ReleaseSemaphore . SYNCHRONIZE , . , Windows NT/2000.
, : main, work, (. ).
!
:
1. main :
− , ;
− ;
− k;
− work;
− SumElement;
− stdout .
− () work;
− SumElement ( , k ).
work ( main - . main, , !):
− , ;
− ( , ). - .
− main ;
− ;
SumElement ( main, !):
− main ;
− k;
− .
2. main :
− , ;
− ;
− work;
− SumElement;
− stdout .
− () work;
work ( main - . main, , !):
− , ;
− ( , ). .
− main ;
− ;
− SumElement ( , .
SumElement ( work,
− work ;
− ;
− .
3. main :
− , ;
− ;