Note: This article is very brief. If you feel difficult to understand, please refer to another article that is much better than mine.
Let's look at an example first
1#include <windows.h>2#include <stdio.h>3 4DWORD WINAPI Reportfunc (LPVOID);//generic prototype of a thread function. You can return no value5 6 #defineCntmsg 1007 8 intMain ()9 {Ten /*creates a new thread and makes it execute immediately, returning the thread's handle One * The third parameter, Reportfunc, represents the function called by this new thread, and different threads can call the same function A * The fourth parameter, NULL, represents the parameter passed to Reportfunc (pointers are recommended). When a parameter is passed, it is converted to a void pointer, which is then transferred by the receiver to the desired type. will be used in the next sample program - * The fifth parameter 0 represents the immediate execution of this thread - */ theHANDLE Reporthandle = CreateThread (NULL,0, Reportfunc, NULL,0, NULL); -CloseHandle (Reporthandle);//courtesy call. Optional - - for(inti =0; i < cntmsg; ++i) printf ("Main Thread:%d\n", i); + -Sleep ( +);//If you do not add this sentence, it may cause the main thread to report the number and exit after the child thread has not yet reported the number of cases + } A at DWORD WINAPI reportfunc (LPVOID para) - { - for(inti =0; i < cntmsg; ++i) printf ("Report Thread:%d\n", i); -}
Its output is probably like this (I do not know what, the results of my first two Tests are very strange: the main thread after the completion of the number of child threads start counting. This is the result of my third Test)
Main thread:0main thread:1main thread:2main thread:3main thread:4main thread:5main thread:6main thread:7main Threa D:8main thread:9main thread:10main thread:11main thread:12main thread:13main thread:14main thread:15main thread:1 6Main thread:17main thread:18main thread:19main thread:20main thread:21main thread:22main thread:23main thread:24m Ain Thread:25main thread:26main thread:27main thread:28main thread:29main thread:30main thread:31main Thread:32mai n thread:33main thread:34main thread:35main thread:36main thread:37main thread:38main thread:39main thread:40main Thread:41main thread:42main thread:43main thread:44main thread:45main thread:46main thread:47main thread:48main Th Read:49main thread:50main thread:51main thread:52main thread:53report thread:0report thread:1report thread:2report Thread:3report thread:4report thread:5report thread:6report thread:7report thread:8report thread:9report thread:1 0Report Thread:11report Thread:12report thread:13report thread:14report thread:15report thread:16report thread:17report thread:18report Th Read:19report thread:20report thread:21report thread:22report thread:23report thread:24report thread:25report Threa D:26report thread:27report thread:28report thread:29report thread:30report thread:31report thread:32report Thread: 33Report thread:34report thread:35report thread:36report thread:37report thread:38report thread:39report thread:40r Eport thread:41report thread:42report thread:43report thread:44report thread:45report thread:46report Thread:47repo RT Thread:48main Thread:54main thread:55main thread:56main thread:57main thread:58main thread:59main thread:60main Thread:61main thread:62main thread:63main thread:64main thread:65main thread:66main thread:67main thread:68main T Hread:69main thread:70main thread:71main thread:72main thread:73main thread:74main thread:75main thread:76main Thr Ead:77main Thread:78maIn Thread:79main thread:80main thread:81main thread:82main thread:83main thread:84main thread:85main thread:86main Thread:87main thread:88main thread:89main thread:90main thread:91main thread:92main thread:93main thread:94main T Hread:95main thread:96main thread:97main thread:98main thread:99report thread:49report thread:50report thread:51re Port Thread:52report thread:53report thread:54report thread:55report thread:56report thread:57report Thread:58repor T thread:59report thread:60report thread:61report thread:62report thread:63report thread:64report thread:65report t Hread:66report thread:67report thread:68report thread:69report thread:70report thread:71report thread:72report Thre Ad:73report thread:74report thread:75report thread:76report thread:77report thread:78report thread:79report Thread: 80Report thread:81report thread:82report thread:83report thread:84report thread:85report thread:86report thread:87 Report Thread:88reportThread:89report thread:90report thread:91report thread:92report thread:93report thread:94report thread:95report Thr Ead:96report Thread:97report Thread:98report thread:99
View Code
The function of this program is to set up a new thread and make it count off with the main thread.
Some problems may occur when several threads access the same variable at the same time. In order to solve this problem, some people invented the mutex. This is a flag that determines if something is not accessible now (that is, it is being accessed by another thread)
Let's take a look at the second example program
1#include <windows.h>2#include <stdio.h>3 4 intTickets = -;//Number of votes5HANDLE Ticketmutex;//the type of mutex is handle6 7 DWORD WINAPI trainstation (LPVOID para)8 {9 Char* StationName = para;//turn type, receive station name informationTen One while(1) A { - //locks the mutex when it waits for the mutex to be freed by another thread - //locking a mutex is equivalent to telling other threads to wait for it to call ReleaseMutex before the tickets variable can be manipulated the WaitForSingleObject (Ticketmutex, INFINITE); - - if(Tickets <=0) Break; -printf"%s>%d\n", StationName,--tickets);//grab the tickets . + - //release the mutex and tell other threads to start manipulating the tickets variable. + ReleaseMutex (Ticketmutex); A } at } - - #defineArrsiz (arr) (sizeof (ARR)/sizeof (ARR[0)) - - intMain () - { in //Use the CreateMutex function to create a mutex - //where the last (third) parameter is an identifier to //If you need to determine if this mutex has been created, you can write code like CreateMutex (NULL, FALSE, "tickets") + //if another mutex with an identifier of "tickets" has been created, then error_already_exists = = GetLastError () -Ticketmutex =CreateMutex (NULL, FALSE, NULL); the * //Station name $ Charstations[][ -] =Panax Notoginseng { - "GuangZhou","Beijing","Shanghai", the "Hunan","Heilongjiang","Xinjiang", + }; A the //Create a sub-thread representing each province station in turn + for(inti =0; I < Arrsiz (stations); ++i) - { $CloseHandle (CreateThread (NULL,0, Trainstation, Stations[i],0, NULL)); $ } - - while(1) the { -WaitForSingleObject (Ticketmutex, INFINITE);//Ibid .Wuyi if(Tickets <=0) Break;//If the tickets are sold out, exit the program theReleaseMutex (Ticketmutex);//Ibid . - } Wu}
Its output is probably the same.
guangzhou> 99guangzhou> 98guangzhou> 97guangzhou> 96guangzhou> 95shanghai> 94GuangZhou> 93shanghai> 92guangzhou> 91heilongjiang> 90hunan> 89shanghai> 88beijing> 87XinJiang> 86GuangZhou > 85heilongjiang> 84hunan> 83shanghai> 82beijing> 81xinjiang> 80guangzhou> 79HeiLongJiang> 78hunan> 77shanghai> 76beijing> 75xinjiang> 74guangzhou> 73heilongjiang> 72HuNan> 71ShangHai> 70beijing> 69xinjiang> 68guangzhou> 67heilongjiang> 66hunan> 65shanghai> 64BeiJing> 63XinJiang > 62guangzhou> 61heilongjiang> 60hunan> 59shanghai> 58beijing> 57xinjiang> 56GuangZhou> 55heilongjiang> 54hunan> 53shanghai> 52beijing> 51xinjiang> 50guangzhou> 49HeiLongJiang> 48HuNan > 47shanghai> 46beijing> 45xinjiang> 44guangzhou> 43heilongjiang> 42hunan> 41ShangHai> 40beijing> 39xinjiang> 38guangzhou> 37heilongjiang> 36hunan> 35ShangHai> 34beijing> 33xinjiang> 32guangzhou> 31heilongjiang> 30hunan> 29shanghai> 28BeiJing> 27xinjiang> 26guangzhou> 25heilongjiang> 24hunan> 23shanghai> 22beijing> 21XinJiang> 20GuangZhou > 19heilongjiang> 18hunan> 17shanghai> 16beijing> 15xinjiang> 14guangzhou> 13HeiLongJiang> 12hunan> 11shanghai> 10beijing> 9xinjiang> 8guangzhou> 7heilongjiang> 6HuNan> 5ShangHai> 4beijing> 3xinjiang> 2guangzhou> 1heilongjiang> 0
View Code
The function of this program is to simulate the train station ticketing system. Each thread simulates the railway stations of each province, competing against each other for 100 tickets, the name of the output province after the ticket, and the remaining number of votes. The main thread monitors the amount of tickets and stops the program after the ticket is sold out.
Note that themutex itself does not know which variable to manage , and whether to manipulate the variable is entirely up to the programmer. That is, if a train station is in dire need of a ticket, it can disregard the mutex and carry out violent robbery. If it's bad luck, When there are several threads accessing the same variable at the same time, the ticket volume is abnormal and so on. If it's lucky, it's not going to happen, just take a few more tickets.
You can also use a mutex to manage multiple variables, depending on your preferences
WINAPI Multithreading (Createthread,createmutex,releasemutex)