TBB record 2

Source: Internet
Author: User

Preemptive Scheduler:

The operating system limits the time for a program to use a processor. When the time is reached, the operating system will force another program to use this processor core. Therefore, the operating system makes the mail program and Web browser seem to run simultaneously.

 

Dividing a problem into multiple processes and using only one restricted and mutually agreed communication method between processes has many advantages. 1. If a process encounters an error, the impact on the entire system is reduced.

 

Process thread: All threads in the process share the data of the process.

 

When writing code:

If the Code cannot be run without the issue of concurrency, the Code cannot be thread-safe ).

In multi-threaded programs, the same function and the same resource may be used concurrently by multiple control flows. Therefore, the code of multi-threaded programs must be writable and thread-safe. At this point, pay attention to the mutex in the thread to ensure that only one thread can execute code with global operations at the same time, and other threads are excluded from this code.

The C ++ library contains some C-mode functions that need to maintain internal states between multiple calls, especially for asctime, ctime, gtime, and localtime, rand, strtok, and other functions. Check the document before use to ensure that they are thread-safe versions. The container class of the C ++ standard template library is usually not thread-safe (therefore, some containers defined in the thread building module are NOT thread-safe ).

 

Concurrent access to the same resource: Use mutex and atomic operations

 

 

The results of different parallel computing sequences are slightly different.

 

Avoid deadlock: Avoid using locks, especially avoid using multiple locks. If the lock must be used: solution 1) obtain the lock in a specific order (for example, first a, then B); 2) release all the locks when one of the locks cannot be obtained, then start again.

 

Conditional competition: the program will not be unable to run, but will cause errors in the program results and will not be easily discovered. Solution: strictly manage the data to ensure the correct order. Avoid using the lock-based low-level method, because this method is prone to errors. The lock is prone to errors and is relatively low-level. Use the synchronization functions implemented by algorithm templates and task schedulers as much as possible. For example, use parallel-Reduce instead of using shared variables. The Join Operation in pralallel_reduce ensures that the operation starts only after the round operation of all sub-problems is completed.

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.