设为首页 加入收藏

TOP

vc++多线程编程
2015-07-22 17:34:51 】 浏览:549
Tags:线程 编程
1.CWinApp是一个i额用户界面线程对象,派生自CWinThread,处理用户产生的事件和消息。

MFC
2.AfxBeginThread函数创建和初始化CWinThread对象,启动并返回地址。

3.
pThread = new CWinThread();
pThread->m_bAutoDelete = FALSE;
pThread = AfxBeginThread(StartAndCloseThreadProc, NULL);

4.
挂起线程
pThread->SuspendThread();
重新启动线程
pThread->ResumeThread();




5.
线程的同步
11.事件对象
#include "stdafx.h"
#include 
  
   
#include 
   
     #include <
    windows.h> #include "process.h" DWORD WINAPI ThreadProc1(LPVOID lpParam); DWORD WINAPI ThreadProc2(LPVOID lpParam); int iGolbalCount=0; int iMax = 12; HANDLE hEvent; int main(int argc, char* argv[]) { HANDLE hThread1, hThread2; hEvent=CreateEvent(NULL,FALSE,FALSE,LPCTSTR("iGolbalCount")); if (hEvent == NULL) { printf("创建事件对象失败!\r\n"); return 0; } SetEvent(hEvent); hThread1=CreateThread(NULL,0,ThreadProc1,NULL,0,NULL); hThread2=CreateThread(NULL,0,ThreadProc2,NULL,0,NULL); Sleep(40000); CloseHandle(hEvent); //关闭事件对象句柄 printf("主线程结束!\n"); system("pause"); return 0; } DWORD WINAPI ThreadProc1(LPVOID lpParam) { while (TRUE) { WaitForSingleObject(hEvent,INFINITE); if (iGolbalCount < iMax) { printf("这里是线程1,iGolbalCount=%d\r\n", iGolbalCount++); SetEvent(hEvent); } else { SetEvent(hEvent); break; } Sleep(10); } return 0; } DWORD WINAPI ThreadProc2(LPVOID lpParameter) { while (TRUE) { WaitForSingleObject(hEvent,INFINITE); if (iGolbalCount < iMax) { printf("这里是线程2,iGolbalCount=%d\r\n", iGolbalCount++); SetEvent(hEvent); } else { SetEvent(hEvent); break; } Sleep(10); } return 0; } 22.利用临界区 DWORD WINAPI ThreadProc1(LPVOID lpParam); DWORD WINAPI ThreadProc2(LPVOID lpParam); int iGolbalCount=0; int iMax = 12; CRITICAL_SECTION cs; int main(int argc, char* argv[]) { HANDLE hThread1, hThread2; InitializeCriticalSection(&cs); hThread1=CreateThread(NULL,0,ThreadProc1,NULL,0,NULL); hThread2=CreateThread(NULL,0,ThreadProc2,NULL,0,NULL); Sleep(50000); DeleteCriticalSection(&cs); //删除临界区对象 printf("主线程结束!\n"); return 0; } DWORD WINAPI ThreadProc1(LPVOID lpParam) { while (TRUE) { EnterCriticalSection(&cs); if (iGolbalCount < iMax) { printf("这里是线程1,iGolbalCount=%d\r\n", iGolbalCount++); LeaveCriticalSection(&cs); } else { LeaveCriticalSection(&cs); break; } Sleep(10); } return 0; } 33.利用信号量 DWORD WINAPI ThreadProc1(LPVOID lpParam); DWORD WINAPI ThreadProc2(LPVOID lpParam); int iGolbalCount=0; int iMax = 12; int cMax = 1; HANDLE hSemaphore; int main(int argc, char* argv[]) { HANDLE hThread1, hThread2; hSemaphore = CreateSemaphore( NULL, cMax, cMax, NULL); if (hSemaphore == NULL) { printf("创建信号量对象失败"); return 0; } hThread1=CreateThread(NULL,0,ThreadProc1,NULL,0,NULL); hThread2=CreateThread(NULL,0,ThreadProc2,NULL,0,NULL); Sleep(5000); printf("主线程结束!"); system("pause"); return 0; } DWORD WINAPI ThreadProc1(LPVOID lpParam) { while (TRUE) { DWORD dwWaitResult = WaitForSingleObject(hSemaphore, 0L); if (dwWaitResult == WAIT_OBJECT_0) { if (iGolbalCount < iMax) { printf("这里是线程1,iGolbalCount=%d\r\n", iGolbalCount++); ReleaseSemaphore(hSemaphore, 1, NULL); } else { ReleaseSemaphore(hSemaphore, 1, NULL); break; } } Sleep(10); } return 0; } 44.利用互斥对象 DWORD WINAPI ThreadProc1(LPVOID lpParam); DWORD WINAPI ThreadProc2(LPVOID lpParam); int iGolbalCount=0; int iMax = 12; HANDLE hMutex; int main(int argc, char* argv[]) { HANDLE hThread1, hThread2; hThread1=CreateThread(NULL,0,ThreadProc1,NULL,0,NULL); hThread2=CreateThread(NULL,0,ThreadProc2,NULL,0,NULL); hMutex=CreateMutex(NULL,TRUE,LPCTSTR("iGolbalCount")); if (hMutex == NULL) { printf("创建互斥对象失败!\r\n"); return 0; } WaitForSingleObject(hMutex,INFINITE); ReleaseMutex(hMutex); ReleaseMutex(hMutex); Sleep(5000); printf("主线程结束!\n"); return 0; } DWORD WINAPI ThreadProc1(LPVOID lpParam) { while (TRUE) { WaitForSingleObject(hMutex,INFINITE); if (iGolbalCount < iMax) printf("这里是线程1,iGolbalCount=%d\r\n", iGolbalCount++); else break; ReleaseMutex(hMutex); Sleep(10); } return 0; } 
   
  



编程开发网
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇VC点击按键弹出文件路径选择对话框 下一篇vc 九宫格贴图

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

最新文章

热门文章

C 语言

C++基础

windows编程基础

linux编程基础

C/C++面试题目