C++多线程的简单程序

// threading.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <windows.h>
using namespace std;

HANDLE hMutex;
volatile int a=0;int b=10;
DWORD WINAPI Fun(LPVOID lpParamter) { 
while(1) { 
WaitForSingleObject(hMutex, INFINITE); 
cout<<"a is "<<++a<<",b is "<< ++b<<endl; 
Sleep(1000); 
ReleaseMutex(hMutex); 
} 
}


int _tmain(int argc, _TCHAR* argv[])
{

HANDLE hThread = CreateThread(NULL, 0, Fun, NULL, 0, NULL); 
hMutex = CreateMutex(NULL, FALSE, LPCWSTR("screen")); 
CloseHandle(hThread); 
while(1) { 
WaitForSingleObject(hMutex, INFINITE); 
cout<<"a is "<<++a<<",b is "<< --b<<endl; 
Sleep(1000); 
ReleaseMutex(hMutex); 
} 
return 0;

}

 

 

 

posted @ 2013-05-02 21:26  lxdonge  阅读(137)  评论(0)    收藏  举报