Welcome to the next pikoTutorial!
Shared mutex
If you’re creating a multithreaded application with tons of read operations and only a few write operations, std::shared_mutex is the way to go. It allows threads to read from the same resource without locking it and at the same time prevents writing to it from more than one thread.
#include <iostream>
#include <mutex>
#include <shared_mutex>
std::shared_mutex mutex;
int value = 0;
void Read()
{
// This line prevents other threads from writing to "value", but allows
// other threads to read from it at the same time
std::shared_lock<std::shared_mutex> lock(mutex);
std::cout << "value = " << value << std::endl;
}
void Write()
{
// This line locks the access to "value", so that no other thread can
// read or write to it
std::unique_lock<std::shared_mutex> lock(mutex);
value = 12;
}Apply
This is a C++ way to perform arguments unpacking. Thanks to unpacking, you can provide multiple function input arguments having only one variable. In Python we have * and in C++ we have std::apply:
#include <iostream>
#include <tuple>
void Function(int a, int b, int c)
{
std::cout << a << " " << b << " " << c << std::endl;
}
int main()
{
const auto args = std::make_tuple(12, 24, 36);
std::apply(Function, args);
}Output:
12 24 36Clamp
std::clamp helps to reduce the amount of boilerplate code because instead of writing such function:
#include <iostream>
static constexpr unsigned int kMinValue = 12U;
static constexpr unsigned int kMaxValue = 24U;
unsigned int GetInRangeValue(unsigned int value)
{
if (value < kMinValue) {
return kMinValue;
}
else if (value > kMaxValue) {
return kMaxValue;
}
else {
return value;
}
}
int main(int argc, char** argv)
{
std::cout << GetInRangeValue(8U) << std::endl;
std::cout << GetInRangeValue(18U) << std::endl;
std::cout << GetInRangeValue(28U) << std::endl;
}You can just use std::clamp:
#include <iostream>
#include <algorithm>
static constexpr unsigned int kMinValue = 12U;
static constexpr unsigned int kMaxValue = 24U;
int main(int argc, char** argv)
{
std::cout << std::clamp(8U, kMinValue, kMaxValue) << std::endl;
std::cout << std::clamp(18U, kMinValue, kMaxValue) << std::endl;
std::cout << std::clamp(28U, kMinValue, kMaxValue) << std::endl;
}Min/Max element
If you need to get both minimum and maximum element from some container, you can do this with a single std::minmax_element function:
#include <algorithm>
int main(int argc, char** argv)
{
const auto elements = {6, 8, 3, 5, 1, 7, 9};
const auto [min_element, max_element] = std::minmax_element(elements.begin(), elements.end());
}
AI is powerful. Snippets are instant.
Stop prompting for the same patterns repeatedly. Get almost 100 free VS Code snippets for C++, Python, CMake and Bazel from piko::snippets GitHub repository.
For each, but limited to a number of elements
To execute some function only on some number of elements, use std::for_each_n:
#include <algorithm>
#include <iostream>
#include <vector>
int main(int argc, char** argv)
{
std::vector<int> elements {6, 8, 3, 5, 4, 7, 9};
std::for_each_n(elements.begin(), 4U, [](int &element) { element = -1; });
}The modified vector is:
-1 -1 -1 -1 4 7 9 Read also:
- 10 VS Code snippets essential for every C++ engineer
- Storing your ATTiny program in…EEPROM?
- Bug of the week #12
- Sharing variable between bash scripts
- A 40-line LLM-based bash command executor in Python
- GTest and short-circuit evaluation in C++
- AI is powerful. Snippets are instant.
- From AUTOSAR to S-Core: the first C++ pub/sub implementation
- How to write Arduino Uno code with Python?
- Combining Bazel with Docker
- Running commands with timeout on Linux
- Running Python unit tests with CMake
- Thirdparty dependencies with FetchContent
- Bug of the week #11
- Combining CMake with Docker
- How to search the internet from Linux terminal?
- Folding expressions in C++
- How to derive from an enum in Python?
- Bug of the week #10
- Trying ROS2: client/server within a single container
- Make C++ a better place #4: Go as an alternative
- How to convert hex to dec in Linux terminal?
- Setting up a Python project with CMake
- Separating builds for different configs with Bazel
- Trying ROS2: pub/sub within a single container
- Bug of the week #9
- UDP multicasting with Python
- Destruction order vs thread safety in C++
- Let’s review some code: C++ #2
- Make C++ a better place #3: D as an alternative
- Registering callback using std::function in C++
- Bug of the week #8
- TCP client/server with Python
- Simple menus in Bash scripts with select
- Calling member function on a nullptr in C++
- Bug of the week #7
- Python lru_cache explained
- How to dockerize a Python application?
- Make C++ a better place #2: CppFront as an alternative
- Parameters combinations in GoogleTest
- Data transfer with curl
- Python reduce explained
- Bug of the week #6
- Custom literals in C++
- Linux and hash command
- 5 Python good practices which make life easier
- Let’s review some code: Python #1
- Make C++ a better place #1: What does better mean
- Enums vs enum class in C++
- Bug of the week #5
- UDP client/server with Python
- Hard links in Linux
- Functions calling order in unit tests in C++
- Bug of the week #4
- Yield in Python – state machines, coroutines and more
- Copy files from another branch with Git
- Make C++ a better place #0: Introduction
- 5 misconceptions about std::move in C++
- How to use xargs on Linux?
- How to test method call order with unittest in Python?









