"Go" "C #" "Thread" mutex mutex

Source: Internet
Author: User

Mutex: Mutex (body) is also called synchronization primitive.

  When an application class is created, a system-wide named mutex object is created at the same time. This mutex is visible throughout the operating system. When a mutex with the same name already exists, the constructor outputs a Boolean value.

  It is mainly applied to control single program operation.

For example, the program code detects this Boolean value to determine whether the specified program instance is started, and if there is already a mutex with the same name, displays a dialog box informing the user that the application has started and exits the application.

Private StaticMutex Mutex =NULL;//set as static member to hold a mutex throughout the program life cycle    Static voidMain () {BOOLfirstinstance; Mutex=NewMutex (true,@"Global\mutexsampleapp", outfirstinstance); The first parameter is permission control, the second parameter is the name of the mutex, and the third parameter indicates whether the mutex can be created, true is the first entry can be created, false means that it already exists, does not allow the creation  of aTry        {            if(!firstinstance) {MessageBox.Show ("an existing instance is running ..."); return; }            Else{MessageBox.Show ("We are the first instance! "); }        }        finally        {            //only the first instance gains control, so ReleaseMutex is required only in this case, otherwise an exception is thrown.             if(firstinstance) {mutex.            ReleaseMutex (); } mutex.            Close (); Mutex=NULL; }    }

Attention:

  In the example, a "global\" prefix is given in the string named for the mutex. This is because on a server running Terminal Services (or Remote Desktop), a named global mutex has two kinds of visibility. If the name begins with the prefix "global\", the mutex is visible in all Terminal Server sessions. If the name begins with the prefix "local\", the mutex is visible only in the Terminal Server session in which it was created, in which case each other Terminal Server session on the server can have a separate mutex with the same name. If you do not specify a prefix when you create a named mutex, it takes the prefix "local\". In a Terminal Server session, only two mutexes with different name prefixes are separate mutexes, and these two mutexes are visible to all processes in a Terminal Server session. That is, the prefix name "global\" and "local\" are used only to describe the scope of the mutex name relative to the Terminal Server session (not relative to the process). Finally, it is important to note that "global\" and "local\" are case-sensitive.

Original address: http://blog.sina.com.cn/s/blog_68e4d2910100q6uj.html

"Go" "C #" "Thread" mutex mutex

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.