The key points of multithreading and concurrency in chromium (C + +)

Source: Internet
Author: User
Tags mutex

category class Description Demo Sample

Threading mechanism

Thread (Reference: Threading model and Application Guide)

Messagepump
MessageQueue
Sequencedworkerpool It is a thread pool that runs task requests that need to be run serially, and these requests are grouped by different tokens, guaranteeing the order of operation only within the same group. This allows multiple groups to run in parallel, while a single group runs serially. For example, the corresponding grouping of different instances is different. or different functions for different groupings. It agrees at the same time to specify how tasks that are not run when exiting are handled, include: Continue running, ignore, block exit. References: The FILE thread is dead, long live the blocking pool.
concurrency control mechanism Volatile

Language is based on the features provided by the processor.

Ensure that each thread reads the data at the most recent value.

But the use of it requires a certain skill.

References: Volatilekeyword Depth analysis

Atomic32

(BASE/ATOMICOPS.H)

Atomicsequencenumber (base/atomic_sequence_num.h)

Atomicrefcountxxx

(base/atomic_ref_count.h)

The atomic data type provided by the chromium.

Autolock

Autounlock

Lock

(base/synchronization/lock.h)

Very close to Java's Synchronized. Lock and Autolock are very well understood.

Autounlock's behavior

Similar to Autolock, when constructed, release lock is acquire lock at the time of destruction.

* Application of Raii idiom.

cookie_manager.cc

Sample Demo for *autounlock:

Media/filters/audio_renderer_impl.h

Waitableevent
(base/synchronization/waitable_event.h)

After an asynchronous call completes, the call ends with a waitableevent waiting for the task to complete.

Implemented based on Lock + conditionvariable.


Conditionvariable
(base/synchronization/condition_variable.h)

C + + implementation of the condition variable. Main methods:

Wait timewait Broadcast Singal

Inprocesscommandbuffer

Cancellationflag
(base/synchronization/cancellation_flag.h)
Atomic operation based. Provides a Boolean flag for setting and querying.
Mechanisms provided by WTF Atomicxxx
(WTF/ATOMICS.H)
Atomic classes provided by WebKit
Mutex
(WTF/TREADINGPRIMITIVES.H)
Implementation of mutually exclusive quantities (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 condition variables
Concurrent containers

Threadlocalboolean

Threadlocalpointer

(base/threading/thread_local.h)

Implementation of TLS (Thread Local stoage) Threadlocal applications in threadrestrictions: Lazyinstance<threadlocalboolean>::leaky g_io_disallowed = LAZY_INSTANCE _initializer;
Threadsafedatatransport
(wtf/threadsafedatatransport.h)
Use Sharedbuffer to pass data between a pair of producers and consumers in a thread-safe manner. Reduce thread conflicts and data copies.

ImageFrameGenerator.h

A lot of other explanations
Lazyinstance

Static member initialization in a function is not thread-safe, Easy is hidden (C++11 has claimed to be guaranteed). can use Base::lazyinstance () to solve, at the same time lazyinstance can avoid memory fragmentation, because its objects are created in the data segment.

References: eliminating static initializers.


Tool class Nonthreadsafe

Only valid under Debug. Provides a protection mechanism for non-thread-safe objects. That is created and used on the same thread.

Main method: Calledonvalidthread ()

Refcountedbase also inherits from Nonthreadsafe in later chromium branches.

Threadcollisionwarner

(Base/threading/thread_collision_warner.h)

Provides a set of macros. Used to help ensure thread safety for classes. This mechanism, which stems from threading problem prevention, prevents threading problems at the coding level.

Details: "Threading Mess".

The main mechanisms provided include:

  1. Dfake_scoped_lock, restricting a function to run on only one thread.
  2. Dfake_scoped_recursive_lock, multiple functions can nest calls on the same thread.
  3. Dfake_scoped_lock_thread_locked, at the same time, just agree that a function runs on the same thread. The containing creation and deallocation should be on the same thread.

  threadchecker
(base/threading/thread _checker.h)

* Only takes effect in debug mode.


  threadrestrictions
(base/threading/ THREAD_RESTRICTIONS.H)

If you do not agree to block I/O, consent to singleton objects, etc.   In the case of a single case. It will be checked at Base::singleton::get ().

  Check for IO is checked in functions of each IO processing, such as loadnativelibrary (),  sysinfo::amountoffreediskspace (),  openfile ( such as  

file* OpenFile ( const filepath& filename, Const Char * mode) {    threadrestrictions::assertioallowed ();    ... }
WatchDog
(base/threading/watchdog.h)
Used to monitor a thread that has not responded at a specified time. The main methods are arm () and disarm ().

In Uma: class shutdownwatchdogthread:public base::watchdog {...}

*c++11 thread is not yet in use.

The key points of multithreading and concurrency in chromium (C + +)

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.