首页 »DotNet » multithread:多线程 MultiThread.cpp »正文
multithread:多线程 MultiThread.cpp
来源: 发布时间:星期五, 2009年1月9日 浏览:64次 评论:0
// MultiThread.cpp : 定义控制台应用 入口点 // # "stdafx.h" # <windows.h> # <iostream> using std; tickets = 100; HANDLE hMutex; DWORD WINAPI Fun1Proc(LPVOID lp); DWORD WINAPI Fun2Proc(LPVOID lp); _t( argc, _TCHAR* argv) { HANDLE hThread1,hThread2; hThread1 = CreateThread(NULL,0,Fun1Proc,NULL,0,NULL); hThread2 = CreateThread(NULL,0,Fun2Proc,NULL,0,NULL); CloseHandle(hThread1); CloseHandle(hThread2); hMutex = CreateMutex(NULL,FALSE,NULL); Sleep(2000); system("pause"); 0; } DWORD WINAPI Fun1Proc(LPVOID lp) { while(1) { WaitForSingleObject(hMutex,INFINITE); (tickets > 0) { Sleep(10); cout << "thread1 sell ticket." << tickets-- <<endl; } ; ReleaseMutex(hMutex); } 0; } DWORD WINAPI Fun2Proc(LPVOID lp) { while(1) { WaitForSingleObject(hMutex,INFINITE); (tickets > 0) cout << "thread2 sell ticket." << tickets-- <<endl; ; ReleaseMutex(hMutex); } 0; }
相关文章
读者评论
发表评论
|
|