互斥量使用实例

来源:互联网 发布:知悉和悉知是什么意思 编辑:程序博客网 时间:2024/06/16 10:05

4种排序公用一个数组~~~使用互斥量防止访问冲突~~

[cpp] view plaincopy
  1. #include <windows.h>  
  2. #include <tchar.h>  
  3. #include <iostream>  
  4. using namespace std;  
  5. const int N=12;  
  6. static int arr[12]={12,50,60,87,98,50,15,31,32,79,21,10};  
  7. HANDLE hMutex;//互斥量句柄  
  8. int data[N];  
  9. void Show(int data[],int n)  
  10. {  
  11.     for(int i=0;i<n;i++)  
  12.         cout<<data[i]<<"/t";  
  13.     cout<<"/n"<<endl;  
  14. };  
  15. void QuickSort(int _data[],int low,int high)//快速排序  
  16. {  
  17.      for(int i=0;i<N;i++)  
  18.           data[i]=_data[i];  
  19.       int i,pivot,j;  
  20.       if(low<high)  
  21.       {  
  22.         pivot = data[low];i=low;j=high;  
  23.        
  24.         while(i<j)  
  25.         {  
  26.           while(i<j&&data[j]>=pivot)   
  27.               j--;  
  28.           if(i<j) data[i++]=data[j];  
  29.           while (i<j&&data[i]<=pivot) i++;  
  30.           if(i<j) data[j--]=data[i];  
  31.         }  
  32.         data[i]=pivot;  
  33.         QuickSort(data,low,i-1);  
  34.         QuickSort(data,i+1,high);  
  35.       }  
  36. };  
  37.     
  38. void MaoPaoSort(int data[])//冒泡排序  
  39. {  
  40.     int list[N];  
  41.     for(int i=0;i<N;i++)  
  42.         list[i]=data[i];  
  43.     int j=1,temp;  
  44.     while((j<N))  
  45.             {  
  46.                 for(int i=0;i<N-j;i++)  
  47.                 {  
  48.                     if(list[i]<list[i+1])  
  49.                     {  
  50.                         temp = list[i];  
  51.                         list[i] = list[i+1];  
  52.                        list[i+1] = temp;  
  53.                     }  
  54.                }  
  55.                j++;  
  56.             }  
  57.     cout<<"MaoPao";  
  58.     Show(list,N);  
  59.       
  60.       
  61. };  
  62. void SortChoice(int data[])//选择排序  
  63. {  
  64.     int list[N];      
  65.     for(int i=0;i<N;i++)  
  66.         list[i]=data[i];  
  67.         int min;  
  68.        for(int i=0;i<N-1;i++)  
  69.        {  
  70.                    min=i;  
  71.               for(int j=i+1;j<N;j++)  
  72.                         {  
  73.                                if(list[j]<list[min])  
  74.                                    min=j;  
  75.                         }  
  76.                     int t=list[min];  
  77.                     list[min]=list[i];  
  78.                     list[i]=t;  
  79.        }  
  80.        cout<<"Choice:";  
  81.     Show(list,N);  
  82. };  
  83. void SortInsert(int data[])//插入排序  
  84. {  
  85.     int list[N];  
  86.     for(int i=0;i<N;i++)  
  87.         list[i]=data[i];  
  88.        for(int i=1;i<N;i++)  
  89.        {  
  90.             int t=list[i];  
  91.             int j=i;  
  92.             while((j>0)&&(list[j-1]<t))  
  93.             {  
  94.              list[j]=list[j-1];  
  95.              --j;  
  96.             }  
  97.             list[j]=t;  
  98.        }  
  99.        cout<<"Insert:";  
  100.    Show(list,N);  
  101. };  
  102. void SortShell(int data[])//希尔排序  
  103. {  
  104.     int list[N];  
  105.     for(int i=0;i<N;i++)  
  106.         list[i]=data[i];  
  107.     int inc;  
  108.        for(inc=1;inc<=N/9;inc=3*inc+1);  
  109.         for(;inc>0;inc/=3)  
  110.        {  
  111.             for(int i=inc+1;i<=N;i+=inc)  
  112.             {  
  113.                int t=list[i-1];  
  114.                int j=i;  
  115.                while((j>inc)&&(list[j-inc-1]>t))  
  116.                {  
  117.                    list[j-1]=list[j-inc-1];  
  118.                    j-=inc;  
  119.                }  
  120.                list[j-1]=t;  
  121.             }  
  122.        }  
  123.         cout<<"Shell:";  
  124.    Show(list,N);  
  125. };  
  126. //线程函数  
  127. DWORD WINAPI Fun1Proc(LPVOID lpParameter)  
  128. {  
  129.     WaitForSingleObject(hMutex,INFINITE);  
  130.     SortShell(arr);  
  131.     ReleaseMutex(hMutex);  
  132.     Sleep(1000);  
  133.     return 0;  
  134. }  
  135. DWORD WINAPI Fun2Proc(LPVOID lpParameter)  
  136. {  
  137.     WaitForSingleObject(hMutex,INFINITE);  
  138.     MaoPaoSort(arr);  
  139.     ReleaseMutex(hMutex);  
  140.     Sleep(1000);  
  141.     return 0;  
  142. }  
  143. DWORD WINAPI Fun3Proc(LPVOID lpParameter)  
  144. {  
  145.     WaitForSingleObject(hMutex,INFINITE);  
  146.     SortChoice(arr);  
  147.     ReleaseMutex(hMutex);  
  148.     Sleep(1000);  
  149.     return 0;  
  150. }  
  151. DWORD WINAPI Fun4Proc(LPVOID lpParameter)  
  152. {  
  153.     WaitForSingleObject(hMutex,INFINITE);  
  154.     SortInsert(arr);  
  155.     ReleaseMutex(hMutex);  
  156.     Sleep(1000);  
  157.     return 0;  
  158. }  
  159.   
  160. int main(int argc,char *argv[])  
  161. {  
  162.     //主线程开始  
  163.     QuickSort(arr,0,11);  
  164.     Show(data,12);  
  165.     Sleep(2000);  
  166.     HANDLE hThread1,hThread2,hThread3,hThread4;//线程句柄  
  167.     //创建线程  
  168.     hThread1=CreateThread(NULL,0,Fun1Proc,NULL,0,NULL);  
  169.     hThread2=CreateThread(NULL,0,Fun2Proc,NULL,0,NULL);  
  170.     hThread3=CreateThread(NULL,0,Fun3Proc,NULL,0,NULL);  
  171.     hThread4=CreateThread(NULL,0,Fun4Proc,NULL,0,NULL);  
  172.       
  173.     //关闭线程句柄  
  174.     CloseHandle(hThread1);  
  175.     CloseHandle(hThread2);  
  176.     CloseHandle(hThread3);  
  177.     CloseHandle(hThread4);  
  178.       
  179.     //创建互斥对象  
  180.     hMutex=CreateMutex(NULL,true,NULL);//true为主线程拥有互斥量,false为当前没有线程拥有互斥量  
  181.     //释放线程对象  
  182.     ReleaseMutex(hMutex);  
  183.       
  184.       
  185.     cin.get();  
  186.     return 0;  
  187. }  

 互斥量的使用方法和临界区(共享代码段)类似,只是互斥量是在内核态实现的,临界区在用户态实现。

0 0
原创粉丝点击