Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Std::mutex detailed explanation of the concurrency Guide of Central11

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article shows you the std::mutex detailed explanation of the concurrency guide of Central11, which is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Mutex, also known as mutexes, Mutex-related classes (including lock types) and functions in C++ 11 are declared in the header file, so if you need to use std::mutex, you must include the header file.

The header file introduces the Mutex series classes (four)

Std::mutex, the most basic Mutex class. Std::recursive_mutex, recursive Mutex class. Std::time_mutex, timed Mutex class. Std::recursive_timed_mutex, timed recursive Mutex class.

Lock classes (two)

Std::lock_guard, related to Mutex RAII, makes it easy for threads to lock mutexes. Std::unique_lock, related to Mutex RAII, makes it easy for threads to lock mutexes, but provides better locking and unlocking control.

Other types

Std::once_flag std::adopt_lock_t std::defer_lock_t std::try_to_lock_t

Function

Std::try_lock, trying to lock multiple mutexes at the same time. Std::lock, which can lock multiple mutexes at the same time. Std::call_once, if multiple threads need to call a function at the same time, call_once can guarantee that multiple threads can only call the function once.

Std::mutex introduction

Let's take std::mutex as an example to introduce the use of mutexes in Clipper 11.

Std::mutex is the most basic mutex in Cellular exclusive 11. The std::mutex object provides exclusive ownership-that is, it does not support recursive locking on std::mutex objects, while std::recursive_lock can recursively lock mutexes.

Member functions of std::mutex

Constructor, std::mutex does not allow copy construction, nor does it allow move copy, and the original mutex object is in the unlocked state. Lock (), the calling thread will lock the mutex. The following three situations occur when a thread calls this function: (1). If the mutex is not currently locked, the calling thread locks the mutex until it is called unlock. (2)。 If the current mutex is locked by another thread, the current calling thread is blocked. (3)。 If the current mutex is locked by the current calling thread, a deadlock occurs. Unlock (), unlock, release ownership of mutexes. Try_lock (), attempts to lock the mutex, and if the mutex is occupied by another thread, the current thread will not be blocked. The following three situations also occur when a thread calls this function, (1). If the current mutex is not occupied by another thread, the thread locks the mutex until the thread calls unlock to release the mutex. (2)。 If the current mutex is locked by another thread, the current calling thread returns false without being blocked. (3)。 If the current mutex is locked by the current calling thread, a deadlock occurs.

Here is a small example with std::mutex (reference)

# include / / std::cout#include / / std::thread#include / / std::mutexvolatile int counter (0); / / non-atomic counterstd::mutex mtx; / / locks access to countervoid attempt_10k_increases () {for (int iTuno; I)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report