C # Advanced Programming reading Notes (15): task, thread, and synchronization four synchronization

Source: Internet
Author: User

Lock statement, the Interlocked class and the monitor class can be used for intra-process synchronization. The mutex class, the event class, the Semaphoreslim class, and the ReaderWriterLockSlim class provide a thread synchronization between threads.

1,interlocked class

The Interlocked class is used to atomically make a simple statement of a variable. The Interlocked class provides a way to increment, decrement, exchange, and read values in a thread-safe manner.

 Public int State        {            get            {                lock (this)                {                    return + + state;}}}        

The above statement can be substituted with the following statement:

 Public int State        {            get            {                return interlocked.increment (ref State );            }        }

2,monitor class

The lock statement is parsed by the C # compiler to use the Monitor class. The following lock statement:

        Lock (obj)        {            //synchroinzed region for obj        }

is resolved to call the Enter () method.

            monitor.enter (obj);             Try             {                //synchroinzed region for obj            }            finally             {                monitor.exit (obj);            }

The main advantage of the monitor class compared to the lock statement in C # is that you can add a timeout that waits to be locked.

            BOOLLockTaken =false; Monitor.TryEnter (obj, -,refLockTaken); if(lockTaken) {Try                {                    //acquired the lock//synchronized region for obj                }                finally{monitor.exit (obj); }            }            Else            {                //Did ' t get the lock,do something else}

3,spinlock structure

If the system overhead of object-based lock objects (Monitor) is too high for garbage collection, you can use the spinlock structure. The spinlock structure is useful if you have a large number of locks and the lock time is always very short.

4,mutex class

A mutex (mutual exclusion, mutex) is a class in the. NET framework that provides synchronous access across multiple processes.

5,semaphore class

Semaphores are very similar to mutexes, and the difference is that semaphores can be used by multiple threads at the same time. The semaphore uses a count of mutex locks. Using semaphores, you can define the number of threads that allow simultaneous access to resources protected by Semaphore lock. Semaphores are useful if you need to limit the number of threads that can access available resources. For example, if the system has 3 physical ports available, you can allow 3 threads to access the I/O port at the same time, but the 4th thread waits for one of the first 3 threads to release resources.

6,events class

As with mutexes and semaphore objects, events are also a system-wide resource synchronization method. You can use events to notify other tasks: There are some data, and some operations have been done. Events can be signaled or not signaled.

C # Advanced Programming reading Notes (15): task, thread, and synchronization four synchronization

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.