VC + + Multithreaded programming

Source: Internet
Author: User

1.CWINAPP is an i-user interface thread object derived from CWinThread that handles user-generated events and messages. MFC2. The AfxBeginThread function creates and initializes the CWinThread object, starting and returning the address. 3.pThread = new CWinThread ();p thread->m_bautodelete = False;pthread = AfxBeginThread (Startandclosethreadproc, NULL) ; 4. Suspend thread Pthread->suspendthread (); Restart thread Pthread->resumethread (); 5. Thread synchronization 11. Event object # include "StdAfx.h" #include <stdio.h> #include <stdlib.h> #include <windows.h> #include "process.h" DWORD WINAPI ThreadProc1 ( LPVOID lpparam);D word 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 ("Create Event object failed! \ 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);//Close Event object handle printf ("Main thread End!\n"); System ("pause"); return 0;} DWORD WINAPI ThreadProc1 (LPVOID lpparam) {while (TRUE) {WaitforsingleobJect (Hevent,infinite), if (Igolbalcount < IMax) {printf ("Here is thread 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 ("Here is thread 2,igolbalcount=%d\r\n", igolbalcount++); SetEvent (hevent);} Else{setevent (hevent); break;} Sleep (10);} return 0;} 22. Using the critical section DWORD WINAPI ThreadProc1 (lpvoid lpparam);D word 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 (&AMP;CS);//delete critical section object printf ("Main thread End!\n"); return 0;} DWORD WINAPI ThreadProc1 (LPVOID lpparam) {while (TRUE) {entercriticalsection (&AMP;CS); if (Igolbalcount < IMax) { printf ("Here is thread 1,igolbalcount=%d\r\n", Igolbalcount++); LeaveCriticalSection (&AMP;CS);} Else{leavecriticalsection (&cs); break;} Sleep (10);} return 0;} 33. Use Semaphore DWORD WINAPI ThreadProc1 (lpvoid lpparam);D word 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, CM AX, NULL); if (Hsemaphore = = NULL) {printf ("Failed to create semaphore object"); return 0;} Hthread1=createthread (Null,0,threadproc1,null,0,null); Hthread2=createthread (Null,0,threadproc2,null,0,null); Sleep (rintf);p ("Main thread end!"); 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 ("Here is thread 1,igolbalcount=%d\r\n", igolbalcount++); ReleaseSemaphore (Hsemaphore, 1, NULL);} Else{releasesemaphore (Hsemaphore, 1, NULL); break;}} Sleep (10);} return 0;} 44. Using the mutex DWORD WINAPI ThreadProc1 (lpvoid lpparam);D word 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 (" Create Mutex failed! \ r \ n "); return 0;} WaitForSingleObject (Hmutex,infinite); ReleaseMutex (Hmutex);    ReleaseMutex (Hmutex); Sleep (rintf);p ("Main thread End!\n"); return 0;} DWORD WINAPI ThreadProc1 (LPVOID lpparam) {while (TRUE) {WaitForSingleObject (hmutex,infinite); if (Igolbalcount < IMax printf ("Here is thread 1,igolbalcount=%d\r\n", igolbalcount++); else break; ReleaseMutex (Hmutex); Sleep (10);} return 0;}


VC + + Multithreaded programming

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.