VC entry book (multi-thread)

zhaozj2021-02-16  48

Multithreading

He Zhidan

main content:

1, worker thread

2, user interface thread

3, synchronize

Threads are divided into worker threads and user user interface threads. The thread of the user interface is that it has a separate message queue that can have its own window interface that can be reacted for user input and events.

A worker thread can be created with the following methods.

Uint MythreadProc (LPVOID PPARAM)

{

...

}

AfxBeginthread (mythread, ..);

It has six parameters, the first for the control function, the second to transmit the inlet parameters of the control function, the current thread, the size of the current thread, the creation status, security attributes of the current thread, The next four have default.

User interface thread:

First use the application wizard to establish a single document program thread, then establish Thread1: Public CWINTHREAD,

Frame1: Public CFrameWnd, you can build these two new classes with Ctrl W.

Add a pointer thread1 * pthread1 in cthreadapp, in Bool CthreadApp :: InitInstance ()

Initialization:

Pthread1 = new thread1 ();

Pthread1-> CreateThread ();

Change the constructor of Thread1 to public.

Add a pointer Frame1 * m_pwnd in Thread1, then initialize.

Bool thread1 :: initInstance ()

{

m_pwnd = new frame1 ();

Return True;

}

Change Frame1's constructor into public, including #include "frame1.h" in thread.h.

Edit a menu idR_Menu in the resource editor, it has a menu item id_begin.

Frame1 :: frame1 ()

{

Create (NULL, "DEMO");

ShowWindow;

UpdateWindow ();

CMenu Menu; // You can use local variables because it will not be used in the future, add menu.

Menu.LoadMenu (IDR_MENU);

SetMenu (& Menu);

}

Synchronize

One difficulty of multi-thread is the coordination of each thread. The same method opens a thread in CTHREADAPP.

Bool cthreadapp :: initInstance ()

{

. . . . . .

Pthread1 = new thread1 ();

Pthread1-> CreateThread ();

Pthread2 = new thread1 ();

Pthread2-> CreateTHread ();

. . . . . .

}

The menu in idR_Menu sets a response function in Frame1, and the method is also Ctrl W Opens the class wizard. And define a global plastic variable N in Frame1, and the initial value is 0.

Handle Handle = CreateSemaphore (NULL, 0, 1, "HE");

WaitforsingleObject (Handle, 10000);

CString Str;

N ;

Str.Format ("Percentage of Double Work", N);

MessageBox (STR);

ReleaseSemaphore (Handle, 1, NULL);

When you click on the menu of Frame1, a dialog box will pop up, temporarily do not point it, click on another thread, you will not pop up the dialog box, determine the dialog box, and another thread is also played.

This synchronization method is called a semaphore. It allows limited threads to access a shared system resource, using counters to implement semaphore. Handle CreateSemaphore (LPSecurity_Attributes LPA, Long Cseminitial, Long Csemmax, LPTSR LPSZSEMNAME);

The first parameter indicates whether the created object can be inherited by its sub-process. If you want to share this signal between all sub-processes, you can set it to True, or you can directly set it to null to use the default security settings second parameters to make several threads. The third parameter is up to several threads.

The last parameter is the name of the semaphore, and the string is used as a parameter when calling createMapphone () or OpenSemaphore () in other processes. Signal handle can be obtained.

ReleaseSemaphore (Handle HSemaphore, Long Crelease, LPLONG PLPREV)

The second means that one release is released.

转载请注明原文地址:https://www.9cbs.com/read-25724.html

New Post(0)