//经典线程同步问题 互斥量Mutex
#include <stdio.h>
#include <process.h>
#include <windows.h>
long g_nNum;
unsigned int __stdcall Fun(void *pPM);
unsigned int __stdcall Fun1(void *pPM);
unsigned int __stdcall Fun2(void *pPM);
const int THREAD_NUM = 10;
//互斥量与关键段
HANDLE g_hThreadParameter;
HANDLE g_hThreadParameter2;
CRITICAL_SECTION g_csThreadCode;
//1.互斥量是内核对象,它与关键段都有“线程所有权”所以不能用于线程的同步。
//
// 2.互斥量能够用于多个进程之间线程互斥问题,并且能完美的解决某进程意外终止所造成的“遗弃”问题。
int main()
{
printf(" 经典线程同步 互斥量Mutex\n");
printf(" -- by MoreWindows( http://blog.csdn.net/MoreWindows ) --\n\n");
//初始化互斥量与关键段 第二个参数为TRUE表示互斥量为创建线程所有
g_hThreadParameter = CreateMutex(NULL, false, NULL);
InitializeCriticalSection(&g_csThreadCode);
g_hThreadParameter2=CreateMutex(NULL,false,NULL);//true 是已在触发状态,不可以触发等待的线程,false 未在触发状态,可以触发等待的线程(event 也是如此)
int i = 0;
_beginthreadex(NULL, 0, Fun2, &i, 0, NULL);
Sleep(20);
HANDLE handle[THREAD_NUM];
g_nNum = 0;
_beginthreadex(NULL, 0, Fun1, &i, 0, NULL);
while (i < THREAD_NUM)
{
handle[i] = (HANDLE)_beginthreadex(NULL, 0, Fun, &i, 0, NULL);
WaitForSingleObject(g_hThreadParameter, INFINITE); //等待互斥量被触发
i++;
}
WaitForMultipleObjects(THREAD_NUM, handle, TRUE, INFINITE);
//销毁互斥量和关键段
CloseHandle(g_hThreadParameter);
Sleep(30000);
DeleteCriticalSection(&g_csThreadCode);
for (i = 0; i < THREAD_NUM; i++)
CloseHandle(handle[i]);
return 0;
}
unsigned int __stdcall Fun1(void *pPM)
{
WaitForSingleObject(g_hThreadParameter2, INFINITE); //等待互斥量被触发
//ReleaseMutex(g_hThreadParameter2);//
int i=0;
while (i<100)
{
printf("线程fun1 %d\t",i++);
}
printf("线程fun1\n");
ReleaseMutex(g_hThreadParameter2);
return 0;
}
unsigned int __stdcall Fun2(void *pPM)
{
WaitForSingleObject(g_hThreadParameter2, INFINITE); //等待互斥量被触发
int i=0;
while (i<100)
{
printf("线程fun2 %d\t",i++);
}
printf("线程fun2*********\n");
while(1)
Sleep(20);//加了这个的作用是为了让程序不退出,目的是验证fun1 与fun2 是互斥的。
//当把这个while循环注释掉 则会输出fun1的打印 说明了<pre code_snippet_id="1943998" snippet_file_name="blog_20161023_1_7749627" name="code" class="cpp">//g_hThreadParameter2 被fun2占据。其他线程不能获得控制权。
</pre><pre code_snippet_id="1943998" snippet_file_name="blog_20161023_1_7749627" name="code" class="cpp">
return 0;}unsigned int __stdcall Fun(void *pPM) { int nThreadNum = *(int *)pPM; ReleaseMutex(g_hThreadParameter);//触发互斥量 Sleep(50);//some work should to do EnterCriticalSection(&g_csThreadCode); g_nNum++; Sleep(0);//some work should to do printf("线程编号为%d 全局资源值为%d\n", nThreadNum, g_nNum); LeaveCriticalSection(&g_csThreadCode); return 0; }
#include <stdio.h>
#include <process.h>
#include <windows.h>
#include <iostream>
using namespace std;
long g_nNum_Semaphore;
unsigned int __stdcall Fun_Semaphore(void *pPM);
const int THREAD_NUM = 60;
//信号量与关键段
HANDLE g_hThreadParameter_Semaphore;
CRITICAL_SECTION g_csThreadCode_Semaphore;
int g_iExcMaxNum=3;
int main()
{
printf(" 经典线程同步 信号量Semaphore\n");
printf(" -- by MoreWindows( http://blog.csdn.net/MoreWindows ) --\n\n");
//初始化信号量和关键段
g_hThreadParameter_Semaphore = CreateSemaphore(NULL, 0, g_iExcMaxNum, NULL);
//当前0个资源,最大允许g_iExcMaxNum个线程同时访问
InitializeCriticalSection(&g_csThreadCode_Semaphore);
HANDLE handle[THREAD_NUM];
g_nNum_Semaphore = 0;
int i = 0;
while (i < THREAD_NUM)
{
handle[i] = (HANDLE)_beginthreadex(NULL, 0, Fun_Semaphore, &i, 0, NULL);
WaitForSingleObject(g_hThreadParameter_Semaphore, INFINITE);//等待信号量>0
++i;
}
WaitForMultipleObjects(THREAD_NUM, handle, TRUE, INFINITE);
Sleep(30000);
int endchar;
cin>>endchar;
//销毁信号量和关键段
DeleteCriticalSection(&g_csThreadCode_Semaphore);
CloseHandle(g_hThreadParameter_Semaphore);
for (i = 0; i < THREAD_NUM; i++)
CloseHandle(handle[i]);
return 0;
}
unsigned int __stdcall Fun_Semaphore(void *pPM)
{
int nThreadNum = *(int *)pPM;
Sleep(100);//some work should to do g_hThreadParameter_Semaphore
int iTreadCall=0;
EnterCriticalSection(&g_csThreadCode_Semaphore);
++g_nNum_Semaphore;
Sleep(0);//some work should to do
printf("线程编号为%d 全局资源值为%d\n", nThreadNum, g_nNum_Semaphore);
LeaveCriticalSection(&g_csThreadCode_Semaphore);
ReleaseSemaphore(g_hThreadParameter_Semaphore, 1, NULL);//信号量++ 这里的参数1 不能大于 g_iExcMaxNum
return 0;
}
总结Mutex和Semaphore内部都是有一个计数器。这个计数器,在使用waitforsingleobject 等等待函数的时候,去和这个计数比较,对于Mutex来说,如果>0 这个内核对象已经被触发了。别的线程wait函数就必须等待这个内核对象被释放,>0 wait 函数会阻塞,等于0 不会阻塞。如果计数等于0 。则说明这个内核对象没有被触发,处于空闲状态,可以去触发。对于Mutex来说wait函数是对计数器加一的作用,realseMutex 是减一,并且减到0 ,不会往下减。对于Semaphore来说waitforsingleobject 会对计数器起到减一的作用,RealseSemaphore有对计数器加一的作用。.>0,wait 函数不会阻塞,wait函数使计数减一,直到0.wai函数会阻塞。要让其不阻塞就必须是计数增加。