C++下关于多线程的应用
来源:互联网 发布:java内存溢出排查 编辑:程序博客网 时间:2024/04/29 02:29
#include <windows.h>
#include <iostream.h>
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);
/**
HANDLE CreateThread(
LPSECURITY_ATTRIBUTES lpThreadAttributes, // SD
SIZE_T dwStackSize, // initial stack size
LPTHREAD_START_ROUTINE lpStartAddress, // thread function
LPVOID lpParameter, // thread argument
DWORD dwCreationFlags, // creation option
LPDWORD lpThreadId // thread identifier
);
*/
CloseHandle(hThread1);//释放句柄,线程执行后没有危险
CloseHandle(hThread2);
/*while(index++<1000)
cout<<"main thread is running"<<endl;*/
hMutex=CreateMutex(NULL,TRUE,NULL);//创建一个互斥对象(mutex),互斥对象包含一个使用数量,一个线程ID和一个计数器。线程ID表示当前拥有互斥对象的线程ID.
/**
HANDLE CreateMutex(
LPSECURITY_ATTRIBUTES lpMutexAttributes, // SD
BOOL bInitialOwner, // initial owner 这是一个信号量,当为FALSE时,表示这个互斥对象没有使用.别得线程可以调用.
LPCTSTR lpName // object name 互斥对象(mutex)名称
);
*/
if(hMutex)//
{
if(ERROR_ALREADY_EXISTS==GetLastError())
{
cout<<"only instance can run!"<<endl;
return;
}
}
/**
上面程序是判断程序是不是只有一个实例在运行,通过互斥对象的反回值来判断.
If the function succeeds, the return value is a handle to the mutex object. If the named mutex object existed before the function call, the function returns a handle to the existing object and GetLastError returns ERROR_ALREADY_EXISTS. Otherwise, the caller created the mutex.
*/
WaitForSingleObject(hMutex,INFINITE);
/**
DWORD WaitForSingleObject(
HANDLE hHandle, // handle to object
DWORD dwMilliseconds // time-out interval
);
Parameters
hHandle
[in] Handle to the object. For a list of the object types whose handles can be specified, see the following Remarks section.
If this handle is closed while the wait is still pending, the function's behavior is undefined.
Windows NT/2000/XP: The handle must have SYNCHRONIZE access. For more information, see Standard Access Rights.
dwMilliseconds
[in] Specifies the time-out interval, in milliseconds. The function returns if the interval elapses, even if the object's state is nonsignaled. If dwMilliseconds is zero, the function tests the object's state and returns immediately. If dwMilliseconds is INFINITE, the function's time-out interval never elapses.
当为INFINITE时为一直等呆资源
*/
ReleaseMutex(hMutex);//释放一个互斥对象(mutex),因为互斥对象(mutex)有计数器,每调用一次这个函数计数器减1
ReleaseMutex(hMutex);//这里释放了两次,是因为创建互斥对象时第二个参数为TRUE,主线程拥有了mutex,下面又用 //WaitForSingleObject(hMutex,INFINITE);申请了一次,ID是一样的,所以主线程的互斥对象计数器为2.必须 //释放两次,要不下面的线程将不会运行.
Sleep(4000);
// Sleep(10);
}
DWORD WINAPI Fun1Proc(
LPVOID lpParameter // thread data
)
{
/*while(index++<1000)
cout<<"thread1 is running"<<endl;*/
/*while(TRUE)
{
WaitForSingleObject(hMutex,INFINITE);
if(tickets>0)
{
Sleep(1);
cout<<"thread1 sell ticket : "<<tickets--<<endl;
}
else
break;
ReleaseMutex(hMutex);
}*/
WaitForSingleObject(hMutex,INFINITE);//线程运行完以后,自动释放互斥对象.
cout<<"thread1 is running"<<endl;
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);
}*/
WaitForSingleObject(hMutex,INFINITE);
cout<<"thread2 is running"<<endl;
return 0;
}
- C++下关于多线程的应用
- linux c 下关于semaphore的使用
- 多线程下关于硬件资源的释放问题
- linux下关于结构体stat的一些应用
- linux下关于结构体stat的一些应用
- Linux下关于时间概念的C语言编程
- Linux下关于时间概念的C语言编程
- windows下关于Objective-C开发环境的配置
- Linux下关于时间概念的C语言编程
- Windows下关于多线程类 CSemaphore,CMutex,CCriticalSection,CEvent,信号量CSemaphore的使用介绍
- VC++下关于工具条的设计
- 找下关于计算机方面的朋友!
- ubuntu 下关于PCAP_FRAMES设置的原文
- Linux下关于内存的查看
- Linux下关于网络设置的文件
- Linux下关于热插拔硬盘的指令
- Linux下关于热插拔硬盘的指令
- Linux下关于NFS的配置步骤
- 信息产业部点名通报 SP业务雪上加霜
- 动态增加按钮
- 泛型學習體會
- 进程的理解
- 我自己学习做的“即时通”
- C++下关于多线程的应用
- 计算机系学生们七条免费的建议和面试题目- by joel
- P2P Multimedia Streaming 技术发展史简介
- 收集 Linux 相关专业术语发音
- SCA/SDO规范的来龙去脉
- .net下可逆的加密算法
- .Net效能優化體會
- html 锚点使用
- 通过rpm包安装、配置及卸载mysql