首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

线程同步测试(一)

2012-11-09 
线程同步测试(1) 老师布置了下课要看一下线程同步,这里主要是实现了五种线程同步:全局变量、临界区、互斥变

线程同步测试(1)

 老师布置了下课要看一下线程同步,这里主要是实现了五种线程同步:全局变量、临界区、互斥变量、信号量、事件

 关于具体的各个用法,个人感觉看<<Windos核心编程>>比较详细。关于具体的介绍和比较,自己会在接下和大家分享。

以下是五种线程同步的测试程序

一 全局变量
/*  项目:线程同步  介绍线程同步-全局变量  函数:  姓名:刘荣  时间: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);//主线程休眠2000CloseHandle(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 3int 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;}



 

热点排行