The Creatthread function looks like this
Here we only use the third and fourth parameters, The third argument passes the address of a function and is the new thread we want to specify. The fourth parameter is a parameter pointer passed to the new thread.
//ThreadCreateTest.cpp: Defines the entry point of the console application. #include "stdafx.h" #include <windows.h>int time = 0;void ThreadProc1 () {int hour = 0;int mins = 0;while (1) {hour = Ti Me/60;mins = Time%60;int Day = hour/24;if (day) {hour-=day*24;} time++;p rintf ("Thread 1%d:%d\n", hour,mins); Sleep (1000); System ("CLS");}} void ThreadProc2 () {int hour = 0;int mins = 0;while (1) {hour = TIME/60; mins = Time%60;int Day = hour/24;//more than 24 hours if (day) {hour-=day*24;} time++;p rintf ("Thread 2%d:%d\n", hour,mins); Sleep (+);//system ("CLS");}} int main () {HANDLE hThread1 = CreateThread (null,0, (lpthread_start_routine) threadproc1,null,0,0); CloseHandle (HTHREAD1); HANDLE hThread2 = CreateThread (null,0, (lpthread_start_routine) threadproc2,null,0,0); CloseHandle (hThread2); GetChar (); return 0;}
Although the above program appears to be concurrent, it is sometimes not necessarily followed by thread 1: Thread 2 output. this involves multi-threaded synchronization issues. For a resource to be pooled by multiple threads can cause confusion in the program, our workaround is to allow only one thread to have exclusive access to the shared resource, which will solve the problem above.
HANDLE CreateMutex (
Lpsecurity_attributeslpmutexattributes,
BOOLBinitialowner,
LPCTSTRlpname);
Parameters
-
-
Lpmutexattributes
-
-
[in] ignored. Must is NULL.
-
-
Binitialowner
-
-
Boolean that specifies the initial owner of the mutex object. If This value is TRUE and the caller created the mutex, the calling thread obtains ownership of the mutex object. Otherwise, the calling thread does not obtain ownership of the mutex. To determine if the caller created the mutex, see the Return Values section.
-
-
Lpname
-
-
[in] Long pointer to a null-terminated string specifying the name of the mutex object. The name is limited-MAX_PATH characters and can contain any character except the backslash path-separator character (\) . Name comparison is case sensitive.
If lpname matches the name of an a existing named mutex object, the binitialowner parameter is ignored Because it has already been set by the creation process.
If lpname is NULL, and the mutex object is created without a name.
If lpname matches the name of an existing event, semaphore, or File-mapping object, the function fails and the GetLastError function returns ERROR_INVALID_HANDLE. This occurs because these objects share the same name space.
The function is used to create an exclusive resource, the first parameter we do not use, can be set to NULL, the second parameter specifies whether the resource is initially attributed to the process that created it, and the third parameter specifies the name of the resource.
HANDLE hmutex;void ThreadProc1 () {int hour = 0;int mins = 0;while (1) {WaitForSingleObject (Hmutex, INFINITE); hour = TIME/60; mins = Time%60;int Day = hour/24;if (day) {hour-=day*24;} time++;p rintf ("Thread 1%d:%d\n", hour,mins); Sleep (1000); ReleaseMutex (Hmutex); System ("CLS");}}
Learn the CreateThread of multi-threaded programming in one step