Concurrent collections of C # Parallel programming (. Net Framework 4.0) (RPM)

Source: Internet
Author: User

Reprint Address: http://blog.csdn.net/wangzhiyu1980/article/details/45497907

This article summarizes and debugs some of the code examples in the article for a personal study of the Advanced tutorial on C # parallel programming. Can be used later in the development process.

For parallel tasks, it is closely related to parallel access to some shared resources, data structures. The most common thing to do is to lock up some queues-unlock them, and then perform mutex operations like insertions, deletions, and so on. NETFramework 4.0 provides a number of packaged support parallel operation data containers that can reduce the complexity of parallel programming.

Basic information

. namespaces for parallel collections in NETFramework: System.Collections.Concurrent

Parallel containers:

    • Concurrentqueue
    • Concurrentstack
    • Concurrentbag: An unordered set of data structures that is useful when you don't need to consider order.
    • BlockingCollection: Similar to the classic blocking queue data structure
    • Concurrentdictionary
These collections use a lock-free technique (CAS compare-and-swap and memory barrier Memories Barrier) to some extent, and gain performance gains over mutex locks. In a serial program, however, it is best not to use these collections, which inevitably affect performance. About CAS:
    • http://www.tuicool.com/articles/zuui6z
    • Http://www.360doc.com/content/11/0914/16/7656248_148221200.shtml
About Memory barriers
    • Http://en.wikipedia.org/wiki/Memory_barrier
Usage and example Concurrentqueue are completely unlocked, but may get stuck in spin and retry operations when CAS face resource contention failures.
    • Enqueue: Inserting elements at the end of a team
    • Trydequeue: Attempt to delete the team header element and return through the out parameter
    • Trypeek: Attempts to return the head element through the out parameter, but does not delete the element.
Example of a program: Concurrentstack It is completely unlocked, but may get stuck in spin and retry when CAs face resource contention failure.
    • Push: Inserts an element into the top of the stack
    • Trypop: Eject element from top of stack and return by out parameter
    • Trypeek: Returns the top element of the stack, but does not eject.
An interesting phenomenon in testing: Although the producer has already inserted a value in the stack to 25, the consumer's first stack is 4, not 25. It's like a mistake. But think about the stack, the stack and print statements are two parts, and not atomic operation, it should be normal to appear in this phenomenon. Concurrentbag an unordered collection in which the program can insert an element, or delete an element. When inserting into a collection in the same thread, it is highly efficient to delete elements.
    • ADD: inserting elements into the collection
    • TryTake: Remove the element from the collection and delete
    • Trypeek: Removes the element from the collection, but does not delete the element.
Example of a program: BlockingCollection a container that supports boundaries and blocking
    • ADD: Inserting elements into the container
    • TryTake: Remove the element from the container and delete
    • Trypeek: Removes the element from the container without deleting it.
    • CompleteAdding: Tells the container to add elements to complete. An exception occurs at this point if you still want to continue adding.
    • IsCompleted: tells the consumer thread that the producer thread is still running and the task is not completed.
Example program: In a program, a consumer thread uses while (!_testbcollection.iscompleted) as a criterion for exiting a run.  In Worker1, two statements are commented out, and when I is 50 the completeadding is set, but when you continue to insert the element, the system throws an exception, prompting that the insertion cannot continue. Of course, you can try to comment out the completeadding statement in Work1, at which point the WORK2 cannot exit. Concurrentdictionary is completely lock-free for read operations, and it uses fine-grained locks when many threads are modifying the data.
    • AddOrUpdate: If the key does not exist, the method adds a new key and value to the container and, if present, updates the existing key and value.
    • Getoradd: If the key does not exist, the method adds a new key and value to the container, returns an existing value if it exists, and does not add a new value.
    • TryAdd: Attempt to add a new key and value to the container.
    • TryGetValue: Attempts to get a value based on the specified key.
    • Tryremove: Attempt to delete the specified key.
    • Tryupdate: Conditionally updates the value corresponding to the current key.
    • GetEnumerator: Returns an enumerator that can traverse the entire container.

Concurrent collections of C # Parallel programming (. Net Framework 4.0) (RPM)

Related Article

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.