Differences between critical sections (Critical section), mutexes (mutexes), semaphores (Semaphore), Events (event)

Source: Internet
Author: User
Tags mutex semaphore

1, critical area: Through the serialization of multithreading to access public resources or a piece of code, fast, suitable for controlling data access. Only one thread is allowed to access the shared resource at any time, and if more than one thread attempts to access the public resource, the other threads that attempt to access the public resource will be suspended after one thread enters, and wait until the thread that enters the critical section leaves and the critical section is freed before other threads can preempt it.

2, Mutex: adopt mutually exclusive object mechanism. Only the thread that owns the mutex has access to the public resource, because there is only one mutex object, so that the public resources are not accessed by multiple threads at the same time. Mutual exclusion can not only realize the common resources security sharing of the same application, but also realize the security sharing of common resources of different applications.

3. Semaphore: It allows multiple threads to access the same resource at the same time, but needs to limit the maximum number of threads that access this resource at the same time

4, Event: The way to keep the thread synchronized by notification operation, also can facilitate the operation of priority comparison of multiple threads.

The role of two parameters in the main function

The first parameter argc is an integer variable, and the second parameter argv is an array of pointers whose elements point to the character type

Data.

Using the main function with parameters, you can get the parameter values directly from the command line (these values are strings), and the program runs

, the corresponding processing can be done according to the different conditions in the input command line. Use the parameters in the main function to make

The program obtains the required data from the system, increasing the flexibility of handling the problem.

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.