为您提供官网链接:
http://www.lenovo.com.cn/?ngAdID=sem_bd_zone_tit_corp_home
更多问题您可以咨询
idea论坛:http://lenovobbs.lenovo.com.cn/forum.php
Think论坛:http://thinkbbs.lenovo.com.cn/forum.php
联想乐社区:http://bbs.lenovomobile.com/forum.php
期待您满意的评价,感谢您对联想的支持,祝您生活愉快!
#include<iostream>
#include <Windows.h>
using namespace std
const int MAX_SEM_COUNT = 5//信号量数量
const int THREAD_COUNT = 10//线程数量
HANDLE g_sem//全局信号量对象句柄
DWORD WINAPI threadFunc(LPVOID)//线程函数前向声明
int main()
{
HANDLE arrThread[THREAD_COUNT]
g_sem = CreateSemaphore(NULL,MAX_SEM_COUNT,MAX_SEM_COUNT,NULL)
if (!g_sem)
{
cout<<"call CreateSemaphore() failed!"<<endl
return -1
}
DWORD threadID = -1
for (int i = 0i <THREAD_COUNT++i)
{
arrThread[i] = CreateThread(NULL, 0, threadFunc, NULL, 0, &threadID)
if (!arrThread[i])
{
cout<<"call CreateThread() failed!"<<endl
return -2
}
}
WaitForMultipleObjects(THREAD_COUNT, arrThread, true, INFINITE)
for (int i = 0i <THREAD_COUNT++i)
{
CloseHandle(arrThread[i])
}
CloseHandle(g_sem)
system("pause")
return 0
}
//线程函数,使用信号量的例子
DWORD WINAPI threadFunc(LPVOID)
{
DWORD waitResult
bool bContinue = true
while (bContinue)
{
waitResult = WaitForSingleObject(g_sem,0)
switch (waitResult)
{
case WAIT_OBJECT_0:
cout <<GetCurrentThreadId()<<" wait sem succeed!"<<endl
bContinue = false
Sleep(5)
if (!ReleaseSemaphore(g_sem, 1, NULL))
{
cout<<"call ReleaseSemaphore() failed!"<<endl
return GetLastError()
}
break
case WAIT_TIMEOUT:
cout <<GetCurrentThreadId()<<" wait sem timeout!"<<endl
break
}
}
}
欢迎分享,转载请注明来源:夏雨云
评论列表(0条)