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;

原创粉丝点击