VC: multi-thread programming

Source: Internet
Author: User

1. Create a worker thread

1. Create a dialog box-based application.
2. Add a button control on the dialog box with the ID ID_START and title "Start worker thread", add a progress bar control, and use the Class Wizard to add the variable CprogressCtrl m_progress;
3. Add the struct in C * Dlg. cpp as follows:
Struct threadInfo
{
Int I;
CProgressCtrl * pctrlProgress;
} Info;
4. Compile a function fun () to implement progress bars.
UINT fun (LPVOID p)
{
ThreadInfo * pInfo = (threadInfo *) p;
For (int I = 0; I <100; I ++)
{
Int nTemp = pInfo-> I;
PInfo-> pctrlProgress-> SetPos (I );
Sleep (nTemp );
}
// PThread = AfxBeginThread (RUNTIME_CLASS (CMyThread ));
Return 0;
}
5. Add a function to create a worker thread.
Void CThreadDlg: OnStart ()
{
// TODO: Add your control notification handler code here
Info. I = 9;
Info. pctrlProgress = & m_progress;
PThread = AfxBeginThread (fun, & Info); // other threads
}

 

2. Create a user interface thread
CWinThread * pThread = AfxBeginThread (RUNTIME_CLASS (CMyThread ));

 

Iii. CEvent Event Sequence

1,

# Include "afxmt. h"

CEvent event;

 

UINT write1 (LPVOID pParam );

UINT write2 (LPVOID pParam );

2,

Void CTestDlg: OnStart ()

{

// TODO: Add your control notification handler code here

CWinThread * pwrite1 = AfxBeginThread (write1,

& M_pro1,

THREAD_PRIORITY_NORMAL,

0,

Create_suincluded );

Pwrite1-> ResumeThread ();

 

CWinThread * pwrite2 = AfxBeginThread (write2,

& M_pro2,

THREAD_PRIORITY_NORMAL,

0,

Create_suincluded );

Pwrite2-> ResumeThread ();

}

 

 

 

UINT write1 (LPVOID pParam)

{

 

CProgressCtrl * p = (CProgressCtrl *) pParam;

For (int I = 0; I <100; I ++)

{

P-> SetPos (I );

Sleep (100 );

}

Event. SetEvent ();

Return 0;

}

UINT write2 (LPVOID pParam)

{

WaitForSingleObject (event. m_hObject, INFINITE); // After annotation

CProgressCtrl * p = (CProgressCtrl *) pParam;

For (int I = 0; I <100; I ++)

{

P-> SetPos (I );

Sleep (100 );

}

Return 0;

}

 

4. Thread communication and Synchronization

 

Note: include the header file: # include "afxmt. h" to add the CSemaphore class.

 

1. Create dialog box

2. Add four progress bar controls, one button, and corresponding variables.

3. Add global variables to * Dlg. cpp.

CCriticalSection section; // the object of the critical section.

CSemaphore semaphore (2, 2); // a maximum of 2, initialize 2;

CEvent event; // event object

4. Add five thread functions:

 

UINT write1 (LPVOID pParam );

UINT write2 (LPVOID pParam );

UINT write3 (LPVOID pParam );

UINT write4 (LPVOID pParam );

UINT write5 (LPVOID pParam );

 

Void CMyDialog: OnStart ()

{

// TODO: Add your control notification handler code here

CWinThread * pWrite1 = AfxBeginThread (write1, & m_pro1, THREAD_PRIORITY_NORMAL, 0, create_suincluded );

PWrite1-> ResumeThread ();

CWinThread * pWrite2 = AfxBeginThread (write2, & m_pro2, THREAD_PRIORITY_NORMAL, 0, create_suincluded );

PWrite2-> ResumeThread ();

CWinThread * pWrite3 = AfxBeginThread (write3, & m_pro3, THREAD_PRIORITY_NORMAL, 0, create_suincluded );

PWrite3-> ResumeThread ();

CWinThread * pWrite4 = AfxBeginThread (write4, & m_pro4, THREAD_PRIORITY_NORMAL, 0, create_suincluded );

PWrite4-> ResumeThread ();

CWinThread * pWrite5 = AfxBeginThread (write5, & m_pro1, THREAD_PRIORITY_NORMAL, 0, create_suincluded );

PWrite5-> ResumeThread ();

}

 

 

UINT write1 (LPVOID pParam)

{

WaitForSingleObject (semaphore. m_hObject, INFINITE );

CProgressCtrl * p = (CProgressCtrl *) pParam;

For (int I = 0; I <100; I ++)

{

P-> SetPos (I );

Sleep (100 );

}

ReleaseSemaphore (semaphore. m_hObject, 1, NULL );

Return 0;

}

UINT write2 (LPVOID pParam)

{

WaitForSingleObject (semaphore. m_hObject, INFINITE );

CProgressCtrl * p = (CProgressCtrl *) pParam;

For (int I = 0; I <100; I ++)

{

P-> SetPos (I );

Sleep (100 );

}

ReleaseSemaphore (semaphore. m_hObject, 1, NULL );

Return 0;

}

UINT write3 (LPVOID pParam)

{

// WaitForSingleObject (semaphoreWrite. m_hObject, INFINITE );

WaitForSingleObject (semaphore. m_hObject, INFINITE );

Section. Lock ();

CProgressCtrl * p = (CProgressCtrl *) pParam;

For (int I = 0; I <100; I ++)

{

P-> SetPos (I );

Sleep (100 );

}

Section. Unlock ();

ReleaseSemaphore (semaphore. m_hObject, 1, NULL );

Return 0;

}

UINT write4 (LPVOID pParam)

{

WaitForSingleObject (semaphore. m_hObject, INFINITE );

Section. Lock ();

CProgressCtrl * p = (CProgressCtrl *) pParam;

For (int I = 0; I <100; I ++)

{

P-> SetPos (I );

Sleep (100 );

}

Section. Unlock ();

ReleaseSemaphore (semaphore. m_hObject, 1, NULL );

Event. SetEvent ();

Return 0;

}

UINT write5 (LPVOID pParam)

{

WaitForSingleObject (event. m_hObject, INFINITE );

CProgressCtrl * p = (CProgressCtrl *) pParam;

For (int I = 0; I <100; I ++)

{

P-> SetPos (I );

Sleep (100 );

}

// ReleaseSmaphore (semaphore. m_hObject, 1, NULL );

Return 0;

}

5. Create a worker thread:

Void CMyDialog: OnStart ()

{

// TODO: Add your control notification handler code here

CWinThread * pWrite1 = AfxBeginThread (write1, & m_pro1, THREAD_PRIORITY_NORMAL, 0, create_suincluded );

PWrite1-> ResumeThread ();

CWinThread * pWrite2 = AfxBeginThread (write2, & m_pro2, THREAD_PRIORITY_NORMAL, 0, create_suincluded );

PWrite2-> ResumeThread ();

CWinThread * pWrite3 = AfxBeginThread (write3, & m_pro3, THREAD_PRIORITY_NORMAL, 0, create_suincluded );

PWrite3-> ResumeThread ();

CWinThread * pWrite4 = AfxBeginThread (write4, & m_pro4, THREAD_PRIORITY_NORMAL, 0, create_suincluded );

PWrite4-> ResumeThread ();

CWinThread * pWrite5 = AfxBeginThread (write5, & m_pro1, THREAD_PRIORITY_NORMAL, 0, create_suincluded );

PWrite5-> ResumeThread ();

}

5. Thread Synchronization

Note: include the header file: # include "afxmt. h" to add the CSemaphore class.

 

1. Create dialog box

2. Add three edit boxes, one button, and corresponding variables.

3. Add global variables to * Dlg. cpp.

CSemaphore semaphoreWrite (3, 3); // semaphore object initialization 3, maximum 3;

Char g_Array [10]; // character array

4. Add three functions:

// Add three functions:

UINT WriteA (LPVOID pParam)

{

CEdit * pEdit = (CEdit *) pParam;

PEdit-> SetWindowText ("");

WaitForSingleObject (semaphoreWrite. m_hObject, INFINITE );

CString str;

For (int I = 0; I <10; I ++)

{

PEdit-> GetWindowText (str );

G_Array [I] = 'a ';

Str = str + g_Array [I];

PEdit-> SetWindowText (str );

Sleep (1000 );

}

ReleaseSemaphore (semaphoreWrite. m_hObject, 1, NULL); // release the semaphore

Return 0;

}

UINT WriteB (LPVOID pParam)

{

CEdit * pEdit = (CEdit *) pParam;

PEdit-> SetWindowText ("");

WaitForSingleObject (semaphoreWrite. m_hObject, INFINITE );

CString str;

For (int I = 0; I <10; I ++)

{

PEdit-> GetWindowText (str );

G_Array [I] = 'B ';

Str = str + g_Array [I];

PEdit-> SetWindowText (str );

Sleep (1000 );

}

ReleaseSemaphore (semaphoreWrite. m_hObject, 1, NULL); // release the semaphore

Return 0;

}

UINT WriteC (LPVOID pParam)

{

CEdit * pEdit = (CEdit *) pParam;

PEdit-> SetWindowText ("");

WaitForSingleObject (semaphoreWrite. m_hObject, INFINITE );

CString str;

For (int I = 0; I <10; I ++)

{

PEdit-> GetWindowText (str );

G_Array [I] = 'C ';

Str = str + g_Array [I];

PEdit-> SetWindowText (str );

Sleep (1000 );

}

ReleaseSemaphore (semaphoreWrite. m_hObject, 1, NULL); // release the semaphore

Return 0;

}

5. Create a synchronization thread for output:

CWinThread * pWriteA = AfxBeginThread (WriteA, & m_A, THREAD_PRIORITY_NORMAL, 0, create_suincluded );

PWriteA-> ResumeThread ();

 

CWinThread * pWriteB = AfxBeginThread (WriteB, & m_ B, THREAD_PRIORITY_NORMAL, 0, create_suincluded );

PWriteB-> ResumeThread (); // end the thread

 

CWinThread * pWriteC = AfxBeginThread (WriteC, & m_C, THREAD_PRIORITY_NORMAL, 0, create_suincluded );

PWriteC-> ResumeThread ();

 

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.