Multi-threaded-5

Source: Internet
Author: User
#include <windows.h>
#include <iostream.h>
//using namespace std;

DWORD WINAPI Fun1proc (
  lpvoid lpparameter   //thread data
);
DWORD WINAPI Fun2proc (
  lpvoid lpparameter   //thread data
);
int index=0;
int tickets=100;
HANDLE Hmutex;
void Main ()
{
    HANDLE hthread1;//thread handle
    HANDLE hThread2;
    Hthread1=createthread (null,0,fun1proc,null,0,null);
    Hthread2=createthread (null,0,fun2proc,null,0,null);
    CloseHandle (HTHREAD1);//closes the handle and does not terminate the thread, indicating that the handle is not interested in the main thread.
                          When the handle is closed, the usage count of the thread kernel object of the new thread is decremented,
                          After the thread executes, the usage count is also decremented//When the usage count is 0 o'clock, the system frees the thread kernel object CloseHandle (hThread2); Hmutex=createmutex (null,false,null);//Create an anonymous mutex object. False no thread owns the mutex,//The operating system sets the mutex to the notified state, which is signaled state sleep (4000);//The main thread abandons the execution power and enters the wait state} Dwor D WINAPI Fun1proc (LPVOID lpparameter) {while (TRUE) {WaitForSingleObject (hmutex,infinite);//The first thread runs, Hmutex is in a signal-like , the thread requests the mutex object//os to set the ID of the mutex object to the thread's ID.
	    When the mutex is obtained,//os sets the mutex to the non-notification state if (tickets>0) {Sleep (1);
	cout<< "Thread1 cell ticket:" <<tickets--<<endl;
	} else break;
ReleaseMutex (Hmutex);//Set the Mutex object ID to 0 and set the mutex to the notified State} return 0;
	} DWORD WINAPI Fun2proc (LPVOID lpparameter) {while (TRUE) {WaitForSingleObject (hmutex,infinite);
		if (tickets>0) {cout<< "Thread2 cell ticket:" <<tickets--<<endl;
	Sleep (1);
	} else break;
    ReleaseMutex (Hmutex); } RETurn 0; }

to create a mutex object function:

HANDLE CreateMutex (

lpsecurity_attributes lpmutexattributes, //SD

BOOL binitialowner, //initial Owner

LPCTSTR lpname//object name

);

Lpmutexattributes:null, default security

Binitialowner: Refers to the original owner of the mutex object. If this value is true, then the caller creates the mutex, and the calling thread obtains ownership of the mutex object, otherwise it does not get ownership.

Lpname: Take a name for the mutex

A mutex is said to be associated with a kernel object, which ensures that the thread has exclusive access to a single resource.

A mutex object contains a usage quantity, a thread ID, and a counter.

The ID is used to identify which thread in the system currently has a mutex, and the counter is used to indicate the number of times that the thread has a mutex.

depending on whether the mutex has been requested, wait:

WaitForSingleObject

The WaitForSingleObject Functionreturns when one of the following occurs:the specified object was in the signaled State. The time-out interval elapses.

To enter a alertable wait state, use the WaitForSingleObjectExfunction. To-wait for multiple objects, use thewaitformultipleobjects.

DWORD WaitForSingleObject (

HANDLE hhandle, //Handleto Object

DWORD dwmilliseconds//time-out interval

);

Hhandle: A handle to a mutex object that becomes signaled and returned by the function. If you are not in a signaled state, the function waits. Waiting causes the thread to run temporarily.

Dwmilliseconds: Refers to the time-out interval. 0, return immediately, INFINITE, wait forever, unless there is a signal

Frees the mutex object-who owns the mutex and who releases it. In this case, the main function is created and can only be released in the main function.

ReleaseMutex

This function releases ownership of thespecified mutex object.

BOOL ReleaseMutex (

HANDLE Hmutex);

Return nonzero indicates success



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.