The difference between Lock,monitor,mutex

Source: Internet
Author: User

There's a lot of difference on the web about these three. But the personal experiment has something to do with what they write. Write the individual's understanding for everyone to make bricks.
Http://www.cnblogs.com/bile/archive/2013/01/08/2850712.html
This article wrote a lot, on the internet a lot of people reproduced. But personal testing does not agree.

The difference between lock and monitor

First, the bottom of lock itself is monitor to achieve, so monitor can implement all the functions of lock.
Second, monitor has tryenter function, can prevent deadlock problem, lock does not.

The difference between a mutex and the other
个人测试三个都是在限制线程之外的互斥,线程之内,都不限制,同一个线程如果被lock两次。是不会出现死锁的。所以Mutex本身可以实现lock和Monitor所有的操作。至少从功能上讲是这样的。但是Mutex是内核级别的,消耗较大的资源,不适合频繁的操作,会降低操作的效率。所以一般被调用部分的资源锁,常常用lock或者Monitor,可以提高效率。而线程和线程间的协调,可以用Mutex,因为相互互斥切换的机会会大大的降低,效率就不再那么的重要了。Mutex本身是可以系统级别的,所以是可以跨越进程的。比如我们要实现一个软件不能同时打开两次,那么Mutex是可以实现的,而lock和monitor是无法实现的。

The difference between Lock,monitor,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.