スレッド同期テスト(1)
7207 ワード
先生は授業が終わると、スレッドの同期を見なければならない.ここでは主に5つのスレッドの同期を実現した.グローバル変数、臨界領域、反発変数、信号量、イベント
具体的な使い方については、個人的には<>の方が詳しいと思います.具体的な紹介と比較については、次から皆さんと共有します.
以下は5種類のスレッド同期のテストプログラムです.
具体的な使い方については、個人的には<
以下は5種類のスレッド同期のテストプログラムです.
グローバル変数
/*
:
-
:
:
:2012/10/22
*/
#include<Windows.h>
#include<iostream>
using namespace std;
int global_value = 0;//
//
DWORD WINAPI Thread_1(LPVOID pParam)
{
cout<<"thread 1 :"<<endl;
//Sleep(200);//
global_value++;
///*
while(1)
{
global_value++;
cout<<"thread_while 1 :global_value = "<<global_value<<endl;
}
//*/
cout<<"thread 1 :global_value = "<<global_value<<endl;
return 0;
}
DWORD WINAPI Thread_2(LPVOID pParam)
{
cout<<"thread 2 :"<<endl;
//Sleep(200);//
global_value++;
///*
while(10)
{
global_value++;
cout<<"thread_while 21 :global_value = "<<global_value<<endl;
}
//*/
cout<<"thread 2 :global_value = "<<global_value<<endl;
return 0;
}
int main()
{
HANDLE hThread_1,hThread_2 ;
//
hThread_1 = CreateThread(NULL,0,
Thread_1,
NULL,0,NULL);
hThread_2 = CreateThread(NULL,0,
Thread_2,
NULL,0,NULL);
Sleep(2000);// 2000
CloseHandle(hThread_1);
CloseHandle(hThread_2);
cout<<"main thread :global_value ="<<global_value<<endl;
return 0;
}
二事件
/*
:
: ——
:CreateThread,CreateEvent,WaitForSingleObject
:
:2012/10/22
*/
#include<Windows.h>
#include<iostream>
using namespace std;
//
HANDLE evRead,evFinish;
//
DWORD WINAPI ReadThread(LPVOID pParam)
{
WaitForSingleObject( evRead, INFINITE);// read
cout<<"Reading"<<endl;
SetEvent(evFinish);//
return 0;
}
DWORD WINAPI WriteThread(LPVOID pParam)
{
//WaitForSingleObject( evRead, INFINITE);// read
cout<<"writing"<<endl;
SetEvent(evRead);//
return 0;
}
int main()
{
evRead = CreateEvent( NULL, FALSE, FALSE,NULL);
evFinish = CreateEvent( NULL, FALSE, FALSE,NULL);
HANDLE hThread_1,hThread_2 ;
//
hThread_1 = CreateThread(NULL,0,
ReadThread,
NULL,0,NULL);
hThread_2 = CreateThread(NULL,0,
WriteThread,
NULL,0,NULL);
//
WaitForSingleObject( evFinish, INFINITE);
return 0;
}
さんりんかいいき
/*
:
: ——
:InitializeCriticalSection,EnterCritaicalSection,LeaveCriticalSection
:
:2012/10/23
*/
#include<Windows.h>
#include<iostream>
using namespace std;
//
int total = 100;
HANDLE evFin[2];
CRITICAL_SECTION cs;
DWORD WINAPI TH_1( PVOID pvParam)
{
EnterCriticalSection(&cs);//
if( total -90 >= 0)
{
total -= 90;
cout<<"You withdrw 90"<<endl;
}
else
{
cout<<"you do not have that much money"<<endl;
}
LeaveCriticalSection(&cs);//
SetEvent( evFin[0]);//
return 0;
}
//
DWORD WINAPI TH_2( PVOID pvParam)
{
EnterCriticalSection(&cs);//
if( total -20 >= 0)
{
total -= 20;
cout<<"You withdrw 20"<<endl;
}
else
{
cout<<"you do not have that much money"<<endl;
}
LeaveCriticalSection(&cs);//
SetEvent( evFin[1]);//
return 0;
}
//
int main()
{
HANDLE hThread_1,hThread_2 ;
evFin[0] = CreateEvent( NULL, FALSE, FALSE,NULL);
evFin[1] = CreateEvent( NULL, FALSE, FALSE,NULL);
//
InitializeCriticalSection(&cs);
//
hThread_1 = CreateThread(NULL,0,
TH_1,
NULL,0,NULL);
hThread_2 = CreateThread(NULL,0,
TH_2,
NULL,0,NULL);
//
WaitForMultipleObjects( 2, evFin, TRUE, INFINITE);
//
DeleteCriticalSection(&cs);
return 0;
}
よんはんぱつりょう
/*
:
: ——
:CreateMutex, ReleaseMutex
:
:2012/10/24
: ?
*/
#include <windows.h>
#include <iostream>
using namespace std;
//
DWORD WINAPI Fun1Proc(
LPVOID lpParameter // thread data
);
DWORD WINAPI Fun2Proc(
LPVOID lpParameter // thread data
);
//
int index=0;
int tickets=100;
HANDLE hMutex;
void main()
{
HANDLE hThread1;
HANDLE hThread2;
hThread1=CreateThread(NULL,0,Fun1Proc,NULL,0,NULL);
hThread2=CreateThread(NULL,0,Fun2Proc,NULL,0,NULL);
CloseHandle(hThread1);
CloseHandle(hThread2);
hMutex=CreateMutexA(NULL,TRUE,"tickets");//
if(hMutex)
{
if(ERROR_ALREADY_EXISTS==GetLastError())//
{
cout<<"only instance can run!"<<endl;
return;
}
}
WaitForSingleObject(hMutex,INFINITE);
ReleaseMutex(hMutex);
ReleaseMutex(hMutex);
Sleep(4000);
}
//
DWORD WINAPI Fun1Proc(
LPVOID lpParameter // thread data
)
{
while(TRUE)
{
//ReleaseMutex(hMutex);
WaitForSingleObject(hMutex,INFINITE);
if(tickets>0)
{
Sleep(1);
cout<<"thread1 sell ticket : "<<tickets--<<endl;
}
else
break;
ReleaseMutex(hMutex);
}
return 0;
}
DWORD WINAPI Fun2Proc(
LPVOID lpParameter // thread data
)
{
while(TRUE)
{
WaitForSingleObject(hMutex,INFINITE);
if(tickets>0)
{
Sleep(1);
cout<<"thread2 sell ticket : "<<tickets--<<endl;
}
else
break;
ReleaseMutex(hMutex);
}
return 0;
}
ごしんごうりょう
/*
:
: ——
:CreateSemaphore,ReleaseSemaphore,OpenSemaphore
:
2012/10/24
*/
#include<Windows.h>
#include<iostream>
using namespace std;
#define THREAD_INSTANCE_NUMBER 3
int golbal = 100;
//
DWORD WINAPI TH( PVOID pvParam)
{
int ThreadNumberTemp = (int)pvParam;
HANDLE hSemaphore;//
cout<<"th :"<<ThreadNumberTemp<<" is runing"<<endl;
if((hSemaphore = OpenSemaphore( SEMAPHORE_ALL_ACCESS, FALSE, L"Semaphore.Text")) == NULL)
{
cout<<"Open Semaphore error"<<endl;
}
cout<<"th :"<<ThreadNumberTemp<<" get the semaphore"<<endl;
while(1)
{
//Sleep(100);
if(golbal >0)
{
cout<<"global = "<<golbal--<<endl;
}
else
{
break;
}
}
ReleaseSemaphore( hSemaphore,1, NULL);//
CloseHandle( hSemaphore);
return 0;
}
int main()
{
DWORD ThreadID[THREAD_INSTANCE_NUMBER];
HANDLE hThread[THREAD_INSTANCE_NUMBER];
HANDLE hSemaphore;
//
if(( hSemaphore = CreateSemaphore( NULL, 0 ,1, L"Semaphore.Text")) == NULL)
{
cout<<"Create Semaphore error!"<<endl;
return 0;
}
for(int i = 0; i<THREAD_INSTANCE_NUMBER; i++)
{
hThread[i] = CreateThread(NULL, 0, TH, &ThreadID[i], 0, &(ThreadID[i]));
if(hThread[i] == NULL)
{
cout<<"CreateThread Error :"<<ThreadID[i]<<endl;
}
else
{
cout<<"Create Thread Success :"<<ThreadID[i]<<endl;
}
}
//
WaitForMultipleObjects(THREAD_INSTANCE_NUMBER, hThread, TRUE, INFINITE);
cout<<"Close the Semaphore Handle"<<endl;
CloseHandle( hSemaphore);
return 0;
}