1、EnterCriticalSection():
等待对共享区域的获取,当获取到此函数立即返回Critical_Section用来对线程之间的同步,在使用之前必须对共享区域进行初始化,InitializeCriticalSection和IniticlizeCriticalSectionAndSpinCount进行初始化
bool WINAPI IniticalizeCriticalSectionAndSpinCount(CRITICAL_SECTION *critialsection,DWORD dwSpinCount)
critialsection:共享资源
dwSpinCount:
当线程进入共享内存,每个线程调用EnterCriticalSection或者TryEnterCriticalSection去请求获取对共享内存的使用权。
EnterCriticalSection:如果没有获得则阻塞直到获取
TryEnterCriticalSection:不管获得与否,立即返回
当一个线程使用完毕后,使用LeaveCriticalSection函数释放使用权,线程可以使用DeleteCriticalSection释放共享资源,释放之后将不能够用于同步。
IniticalizeCriticalSectionAndSpinCount(&CriticalSection,0);
//请求
EnterCriticalSection(&Critical);
ReleaseCriticalSection(&Critical);
//释放资源
DeleteCriticalSection(&Critical);
2、CreateMutex
HANDLE WINAPI CreateMutex(
LPSECURITY_ATTRIBUTES lpMutexAtteibutes,
BOOL bInitialOwner,
LPCTSTR loName
)
lpMutexAttributes:指向SECURITY_ATTRIBUTES,如果为0,则handle不能为子进程继承
bInitialOwner:TRUE表示调用者拥有Mutex对象
lpName:NULL,或者填
返回:新的handle,失败返回NULL,用GetLastError()
3、CreateSemaphore
HANDLE WINAPI CreateSemaphore(
LPSECURITY_ATTRIBUTES lpSemaphoreAttributes,
LONG LInitialCount,
LONG LMximumCount,
LPCTST lpName
)
lpSemaphoreAttributes:指向SECURITY_ATTRIBUTES,为零不被子进程进程
IInitialCount:初始值
IMaximumCount:最大值
lpName:名字
等待对共享区域的获取,当获取到此函数立即返回Critical_Section用来对线程之间的同步,在使用之前必须对共享区域进行初始化,InitializeCriticalSection和IniticlizeCriticalSectionAndSpinCount进行初始化
bool WINAPI IniticalizeCriticalSectionAndSpinCount(CRITICAL_SECTION *critialsection,DWORD dwSpinCount)
critialsection:共享资源
dwSpinCount:
当线程进入共享内存,每个线程调用EnterCriticalSection或者TryEnterCriticalSection去请求获取对共享内存的使用权。
EnterCriticalSection:如果没有获得则阻塞直到获取
TryEnterCriticalSection:不管获得与否,立即返回
当一个线程使用完毕后,使用LeaveCriticalSection函数释放使用权,线程可以使用DeleteCriticalSection释放共享资源,释放之后将不能够用于同步。
具体流程例如:
CRITICAL_SECTION CriticalSection;
//初始化IniticalizeCriticalSectionAndSpinCount(&CriticalSection,0);
//请求
EnterCriticalSection(&Critical);
ReleaseCriticalSection(&Critical);
//释放资源
DeleteCriticalSection(&Critical);
2、CreateMutex
HANDLE WINAPI CreateMutex(
LPSECURITY_ATTRIBUTES lpMutexAtteibutes,
BOOL bInitialOwner,
LPCTSTR loName
)
lpMutexAttributes:指向SECURITY_ATTRIBUTES,如果为0,则handle不能为子进程继承
bInitialOwner:TRUE表示调用者拥有Mutex对象
lpName:NULL,或者填
返回:新的handle,失败返回NULL,用GetLastError()
3、CreateSemaphore
HANDLE WINAPI CreateSemaphore(
LPSECURITY_ATTRIBUTES lpSemaphoreAttributes,
LONG LInitialCount,
LONG LMximumCount,
LPCTST lpName
)
lpSemaphoreAttributes:指向SECURITY_ATTRIBUTES,为零不被子进程进程
IInitialCount:初始值
IMaximumCount:最大值
lpName:名字
返回:新的handle,失败返回NULL
4、WaitForSingleObject
DWORD WINAPI WaitForSingleObject(HANDLE handle,DWORD dwMillseconds)
handle:等待对象
dwMillseconds:等待时间,单位毫秒。为0函数立即返回,INIFINITE一直等待
返回值:
WAIT_ABANDONED:互斥锁等未被释放,阻塞
WAIT_OBJECT_0:等待的信号出现
WAIT_TIMEOUT:超时
#include <Windows.h>
DWORD WINAPI ThreadFunc(LPVOID);
HANDLE ghSemaphore;
HANDLE ghMutex;
DWORD WINAPI WriteToDatabase(LPVOID);
int main(int argc, char *argv[])
{
ghSemaphore=CreateSemaphore(NULL,1,10,NULL);
if(ghSemaphore==NULL){
printf("CreateSemaphore error:%d\n",ghSemaphore);
return 0;
}
HANDLE hThrd;
DWORD threadId1;
int i;
for(i=0;i<1;i++){
hThrd=CreateThread(NULL,0,ThreadFunc,(LPVOID)i,0,&threadId1);
if(hThrd){
printf("Thread launched %d\n",i);
CloseHandle(hThrd);
}
}
char st[13]={0};
while(fgets(st,13,stdin))
{
if(strcmp(st,"n\n")==0)
{
ReleaseSemaphore(ghSemaphore,1,NULL);
}
}
HANDLE aThread[5];
DWORD ThreadID2;
ghMutex=CreateMutex(NULL,FALSE,NULL);
if(ghMutex==NULL)
{
printf("CreateMutex error:%d\n",GetLastError());
}
for(int i=0;i<5;i++){
aThread[i]=CreateThread(NULL,NULL,WriteToDatabase,NULL,0,&ThreadID2);
if(aThread[i]==NULL){
printf("CreateThread error:%d\n",GetLastError());
return 0;
}
}
WaitForMultipleObjects(5,aThread,TRUE,INFINITE);
for(int i=0;i<5;i++){
CloseHandle(aThread[i]);
}
CloseHandle(ghMutex);
getchar();
return 0;
}
DWORD WINAPI WriteToDatabase(LPVOID)
{
DWORD dwCount=0,dwWaitResult;
while(dwCount<5){
dwWaitResult=WaitForSingleObject(ghMutex,INFINITE);
switch(dwWaitResult)
{
case WAIT_OBJECT_0:
_try{
printf("Thread %d writing to the database...\n",GetCurrentThreadId());
dwCount++;
}
_finally{
if(!ReleaseMutex(ghMutex)){
//
}
}
break;
case WAIT_ABANDONED:
return FALSE;
}
}
return TRUE;
}
DWORD WINAPI ThreadFunc(LPVOID n){
DWORD dwWaitResult;
BOOL bContinue=TRUE;
while(bContinue)
{
dwWaitResult=WaitForSingleObject(ghSemaphore,1000);
switch (dwWaitResult)
{
case WAIT_OBJECT_0:
printf("Thread %d: wait signaled\n",GetCurrentThreadId());
Sleep(2500);
if(!ReleaseSemaphore(ghSemaphore,1,NULL)){
printf("ReleaseSemaphore error:%d \n",GetLastError());
}
break;
case WAIT_TIMEOUT:
printf("Thread %d: wait timed out\n",GetCurrentThreadId());
break;
}
}
return TRUE;
}
没有对具体的机制进行解释,等过一段时间测试补充