The createthread API function can be used to create a thread. The following is a function prototype:
Handle createthread (<br/> lpsecurity_attributes lpthreadattributes, // pointer to security attributes <br/> DWORD dwstacksize, // initial thread stack size <br/> lpthread_start_routine lpstartaddress, // pointer to thread function <br/> lpvoid lpparameter, // argument for new thread <br/> DWORD dwcreationflags, // creation flags <br/> lpdword lpthreadid // pointer to receive thread id <br/>); <br/>
After creating a thread, the thread is required to execute some work. At this time, the execution work is handed over to the function of the third parameter thread above for implementation.
The definition of thread functions is as follows:
DWORD winapi threadproc (<br/> lpvoid lpparameter // thread data <br/>); </P> <p>
The parameter in the thread function is the fourth parameter in the process function.
When creating a thread, the parameter is directly passed to the thread function.
The following example shows the working process of the thread.
# Include "stdafx. H "<br/> # include <windows. h> <br/> # include <stdio. h> <br/> // declare two thread functions <br/> // shop1, shop2 simulates two ticket sales tickets <br/> DWORD winapi shop1 (lpvoid lpparameter); <br/> DWORD winapi shop2 (lpvoid lpparameter ); <br/> // 200 flights for sale <br/> int tickets = 200; <br/> int main () <br/>{< br/> // enable two threads to start ticket sales <br/> handle hthread1 = createthread (<br/> null, // set to null, it indicates that the handle cannot be inherited <br/> 0, // 0 indicates that it is automatically allocated by Windows <br/> shop1, // The specified thread function <br/> null, // parameter to be passed to the thread function <br/> 0, // if you want to suspend the thread after it is created (that is, it is not running ), it needs to be set to create_suincluded <br/> null); // null indicates no thread id <br/> handle hthread2 = createthread (null, 0, shop2, null, 0, null); <br/> // delay exit <br/> sleep (10000); <br/> // reduce the application count <br/> closehandle (shop1 ); <br/> closehandle (shop2); <br/> return 0; </P> <p >}< br/> DWORD winapi shop1 (lpvoid lpparameter) <br/> {<br/> // cyclically judge tickets. If the value is greater than 0, the ticket is not sold out. <br/> while (1) <br/>{< br/> If (tickets> 0) <br/>{< br/> printf ("shop1 should a ticket: % d/N ", tickets); <br/> tickets --; <br/>}< br/> else // exit after it is sold out <br/>{< br/> return 0; <br/>}< br/> return 0; <br/>}</P> <p> DWORD winapi shop2 (lpvoid lpparameter) <br/> {<br/> // cyclically judge tickets. If the value is greater than 0, the ticket is not sold out. <br/> while (1) <br/>{< br/> If (tickets> 0) <br/>{< br/> printf ("shop1 should a ticket: % d/N ", tickets); <br/> tickets --; <br/>}</P> <p> else // exit after selling out. <br/>{< br/> return 0; <br/>}< br/> return 0; <br/>}
In fact, it is very simple. After creating a thread through a thread function and implementing a thread function, you can work.
----------------------------------------------------------
During the execution of multi-threaded programs, multiple threads will access the same piece of data, leading to street resources. In order to prevent them from accessing the site at the same time and cause uncertain impact, the street-facing resources need to be protected. This process is called thread synchronization.
We can implement this by creating mutex objects.
Createmutex
Handle createmutex (<br/> lpsecurity_attributes lpmutexattributes, <br/> // pointer to security attributes <br/> bool binitialowner, // flag for Initial ownership <br/> lpctstr lpname // pointer to mutex-object name <br/>); <br/>
For specific implementation, see the following example:
// Multithead_selltickets.cpp: defines the entry point for the application. <br/> // # include "stdafx. H "<br/> # include <windows. h> <br/> # include <stdio. h> <br/> // declare two thread functions <br/> // shop1, shop2 simulates two ticket sales tickets <br/> DWORD winapi shop1 (lpvoid lpparameter); <br/> DWORD winapi shop2 (lpvoid lpparameter ); <br/> // There are 200 flights for sale <br/> int tickets = 200; <br/> //////////////////////////////////// /////////////////// ///// // <Br/> // solve the problem of simultaneously occupying the same critical resource by means of thread synchronization, 1, use mutex object 2, event object, 3, critical section <br/> // Add mutex object (global) <br/> handle g_hmutex = NULL; </P> <p> ///////////////////////////////// /// // <br/> int main () <br/> {<br/> /////////////////////////////// //////////////////////////////////////// /// <br/> // before the thread is created, create a mutex first, and use the common parameter <br/> g_hmutex = createmutex (<br/> null, // inheritance is not allowed <br/> false, // The creation thread waives the ownership. <br /> Null ); // No Name </P> <p> ///////////////////////////// //////////////////////////////////////// ///// <br/> // enable two threads to start ticket sales <br/> handle hthread1 = createthread (<br/> null, // if it is set to null, the handle cannot be inherited. <br/> 0, // 0 indicates that it is automatically allocated by windows. <br/> shop1, // specify the thread function <br/> null, // parameter to be passed to the thread function <br/> 0, // if you want to suspend a thread after it is created (that is, it is not running), you need to set it to create_suincluded <br/> null ); // null indicates that no thread ID is required. <br/> handle hthread2 = createthread (null, 0, shop2, null, 0, null); <br/> // extend </P> <p> sleep (10000); <br/> // reduce application count <br/> closehandle (shop1 ); <br/> closehandle (shop2); <br/> return 0; </P> <p >}< br/> DWORD winapi shop1 (lpvoid lpparameter) <br/> {<br/> // cyclically judge tickets. If the value is greater than 0, the ticket is not sold out. <br/> while (1) <br/> {<br/> /////////////////////////////// //////////////////////////////////////// /// <br/>/* waitforsingleobject waits for an object to reach the signal state <br/> when the owner of a mutex object waives its ownership <br/> after waitforsingleobject is called successfully the thread is its ownership. Owner <br/> the owner must use releasemutex or end the thread before giving up the ownership of the owner <br/> */<br/> waitforsingleobject (<br/> g_hmutex, // indicates the object to wait <br/> infinite); // indicates the time to wait, and infinite indicates unlimited... <Br/> If (tickets> 0) <br/> {<br/> sleep (1); // latency: when tickets = 0 is displayed, this is because at the same time, temporary resources are occupied .. <Br/> printf ("shop1 requires a ticket: % d/N", tickets); <br/> tickets --; <br/>}</P> <p> else // exit after selling out. <br/>{< br/> return 0; <br/>}< br/> releasemutex (g_hmutex); <br/>}< br/> return 0; <br/>}</P> <p> DWORD winapi shop2 (lpvoid lpparameter) <br/>{< br/> // judge tickets cyclically. If> 0, the ticket is not sold out <br/> while (1) <br/>{< br/> waitforsingleobject (<br/> g_hmutex, // indicates the object to wait <br/> infinite); // indicates the time to wait, and infinite indicates unlimited... <Br/> If (tickets> 0) <br/> {<br/> printf ("shop2 should a ticket: % d/N", tickets ); <br/> tickets --; <br/>}</P> <p> else // exit after selling out. <br/>{< br/> return 0; <br/>}< br/> releasemutex (g_hmutex); <br/>}< br/> return 0; <br/>}< br/>
-------------------------------------
In addition to mutex resources, the thread synchronization method also has other methods. The principles and methods are similar.
During thread synchronization, some methods will be called to cause other threads to wait. In fact, there are also potential risks. That is to say, for some reason, the system keeps waiting, causing a deadlock.
To avoid deadlocks, You need to develop good programming specifications and make the program reasonable in design.