Key Technical Points of multithreading and concurrency in Chromium (C/C ++)

Source: Internet
Author: User

Key Technical Points of multithreading and concurrency in Chromium (C/C ++)

Category Class Description Example

Thread Mechanism

Thread (Reference: thread Model and Application Guide)
 
MessagePump  
MessageQueue  
SequencedWorkerPool It is a thread pool used to execute task requests that require serial execution. These requests are grouped by different Token groups and only ensure the execution order in the same group. In this way, multiple groups can be executed in parallel, while a single group is executed in serial mode. For example, different instances correspond to different groups, or different functions correspond to different groups. It also allows you to specify how tasks that are not executed when they exit, including: continue execution, ignore, and stop exiting. Reference: The FILE thread is dead, long live the blocking pool.  
Concurrency Control Mechanism Volatile

The language is based on the features provided by the processor. Ensure that the data read by each thread is the latest value. However, it requires some skills.

Reference: in-depth analysis of C/C ++ Volatile keywords

 
 

Atomic32

(Base/atomicops. h)

AtomicSequenceNumber (base/atomic_sequence_num.h)

AtomicRefCountXxx

(Base/atomic_ref_count.h)

The atomic data type provided by Chromium.  
 

AutoLock

AutoUnlock

Lock

(Base/synchronization/lock. h)

Very similar to JavaSynchronized. Lock and AutoLock are easy to understand. AutoUnlock action

Similar to AutoLock, release lock during construction and acquire lock during analysis.

* RAII idiom is applied.

Cookie_manager.cc

* Example of AutoUnlock:

Media/filters/audio_renderer_impl.h

 
  WaitableEvent
(Base/synchronization/waitable_event.h)

The operation is completed with an asynchronous call, and the call end waits for the task to be completed with a WaitableEvent.

Implemented based on Lock + ConditionVariable.

  ConditionVariable
(Base/synchronization/condition_variable.h)

C/C ++ Implementation of conditional variables. Main Methods:

WaitTimeWaitBroadCastSingal

InProcessCommandBuffer

 
  CancellationFlag
(Base/synchronization/cancellation_flag.h)
Provides setting and query of a Boolean flag Based on atomic operations.  
Mechanisms provided by WTF AtomicXXX
(Wtf/Atomics. h)
Atomic classes provided by WebKit  
  Mutex
(Wtf/TreadingPrimitives. h)
Implementation of mutex (abstraction of the Platform)
Similar to the Lock series in base.

Mutex m_mutex;

{
MutexLocker locker (m_mutex );

......

}

 

MutexLock

(Wtf/TreadingPrimitives. h)

  RecursiveMutex
(Wtf/TreadingPrimitives. h)
  MutexTryLocker (wtf/TreadingPrimitives. h)
  ThreadCondition
(Wtf/TreadingPrimitives. h)
Implementation of conditional variables  
Concurrent container

ThreadLocalBoolean

ThreadLocalPointer

(Base/threading/thread_local.h)

Implementation of TLS (Thread Local Stoage) Applications with ThreadLocal in ThreadRestrictions: LazyInstance : Leaky G_io_disallowed = LAZY_INSTANCE_INITIALIZER;
  ThreadSafeDataTransport
(Wtf/ThreadSafeDataTransport. h)
The SharedBuffer is used to transmit data between a producer and a consumer in a thread-safe manner. Reduces thread conflicts and data copies.

ImageFrameGenerator. h

More instructions
  LazyInstance

Static member initialization in a function is not thread-safe and is prone to risks (C ++ 11 has claimed to be guaranteed ). You can use base: LazyInstance () to solve the problem. At the same time, LazyInstance can avoid memory fragmentation because its objects are all created in the data segment.

Refer to: Eliminating static initializers.

 

 
Tool NonThreadSafe

Valid only in Debug. Provides a protection mechanism for non-thread security objects. That is, it is created and used on the same thread.

Main method: CalledOnValidThread ()

RefCountedBase also inherits from NonThreadSafe in the latest Chromium branch.

 
 

ThreadCollisionWarner

(Base/threading/thread_collision_warner.h)

Provides a set of macros to help ensure the thread security of the class. This mechanism stems from thread issue prevention and prevents thread problems at the encoding level. For details, see "Threading mess ".

The main mechanisms provided include:

DFAKE_SCOPED_LOCK: limits that a function can only run on one thread. DFAKE_SCOPED_RECURSIVE_LOCK. Multiple functions can be nested in the same thread. DFAKE_SCOPED_LOCK_THREAD_LOCKED. At the same time, only one function can run on the same thread. Including creation and release should be on the same thread.
  ThreadChecker
(Base/threading/thread_checker.h)

For a non-thread-safe class, a ThreadChecker member variable can be used to ensure that its instance is not operated across threads.

* It takes effect only in Debug mode.

  ThreadRestrictions
(Base/threading/thread_restrictions.h)
Add constraints for each thread. For example, blocking I/O is not allowed, and whether a singleton object is allowed. Taking Singleton as an example, it will check in base: Singleton: get. IO checks are performed in various IO processing functions, such as LoadNativeLibrary (), SysInfo: AmountOfFreeDiskSpace (), and OpenFile.
FILE* OpenFile(const FilePath& filename, const char* mode) {ThreadRestrictions::AssertIOAllowed();…...}
  WatchDog
(Base/threading/watchdog. h)
It is used to monitor situations where a thread does not respond at a specified time. The main methods are Arm () and Disarm (). In UMA: class ShutdownWatchDogThread: public base: Watchdog {......}

* C ++ 11 thread is not used yet.

 

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.