Stay up-to-date with everything C++! Content directly fetched from the subreddit just for you. Join our group for discussions : @programminginc Powered by : @r_channels
Tetris Wall Art for Arduino
https://www.codeproject.com//Articles/5378514/Tetris-Wall-Art-for-Arduino
https://redd.it/1b6ytn9
@r_cpp
Cpp learning checklist for network programming
Hi people, I want to know if there's any complete list present online that will lead me to learn network programming using c++. Tbh, I don't know what projects to to take on to enhance my CPP learning just came across network programming so was wondering if I can make a checklist out of the necessary concepts.
https://redd.it/1b6zdgl
@r_cpp
Introduction To Low Latency Programming: Understand Storage
https://tech.davidgorski.ca/introduction-to-low-latency-programming-understand-storage/
https://redd.it/1b6g6wl
@r_cpp
C++ Videos Released This Month - March 2024
This month the following C++ videos have been published to YouTube. A new post will be made each week as more videos are released
**CppCon**
02/26/2024 - 03/03/2024
* Leveraging the Power of C++ for Efficient Machine Learning on Embedded Devices - Adrian Stanciu - [https://youtu.be/5j05RWh1ypk](https://youtu.be/5j05RWh1ypk)
* C++ Regular, Revisited - Victor Ciura - [https://youtu.be/PFI\_rpboj8U](https://youtu.be/PFI_rpboj8U)
* Evolution of a Median Algorithm in C++ - Pete Isensee - [https://youtu.be/izxuLq\_HZHA](https://youtu.be/izxuLq_HZHA)
* Back to Basics: The Rule of Five in C++ - Andre Kostur - [https://youtu.be/juAZDfsaMvY](https://youtu.be/juAZDfsaMvY)
* C++23: An Overview of Almost All New and Updated Features - Marc Gregoire - [https://youtu.be/Cttb8vMuq-Y](https://youtu.be/Cttb8vMuq-Y)
All of these talks can also be accessed at [https://cppcon.programmingarchive.com](https://cppcon.programmingarchive.com) where you can also find information on how to get early access to the rest of the CppCon 2023 videos and lightning talks.
https://redd.it/1b6f7hb
@r_cpp
Any news on when libc++ is going to support std::expected?
According to cppreference, libc++ supports std::expected starting with version 16, though a very quick check on Compiler Explorer shows this is not the case. GCC 13 supports it, though it means if you're using Clangd as an LSP you'll get lots of superfluous errors and can impact the actual errors it can report on.
https://redd.it/1b6f3s4
@r_cpp
Any good C/C++ AI projects out there?
Everyone is giving python the AI love I would like to get into AI development but all in C/C++. I personaly hate the python way of doing things. If there's any open source AI projects out there for C/C++ that would be cool to play with. chat bots, image bots.
https://redd.it/1b6aufv
@r_cpp
Email verify
I'm trying to code a register protocol in c++, but the email verify insn't working. I tried so much things but it still not working. (I'm trying to verify based on carachters). What should i do?
include <iostream>
include <locale.h>
include <string.h>
include <stdio.h>
using namespace std;
char email[50\], ;
int arr, pon, tam, pas, eml, d_a, d_p;
int main(int argc, char** argv){
pas = 0;
cout << "Digite seu email:\\n";
//erro down here
while(pas < 1){
cin >> email;
eml = strlen(email);
arr = 0;
pon = 0;
tam = 0;
if(eml >= 3){
tam = 1;
}
if (email[50\] == '@'){
arr = 1;
}
if (email[50\] == '.'){
pon - 1;
}
if (arr == 1){
d_a = 1;
}
if (pon == 1){
d_p = 1;
}
if (pon == 1 && arr == 1 && d_a == 1 && d_p == 1){
pas = 1;
}
else
{
cout << "Este email é inválido";
}
}
}
https://redd.it/1b603zh
@r_cpp
Digital signal processing for microcontrollers in C++ ? Where to start ?
Im fairly new to the subject. Want to build a guitar pedal as a side project to learn about microcontrollers, communication protocols and signal processing. My goal is to dive deep and learn the background about these concepts as much as possible so dont want to use a library full of steroids like JUCE. I should be able to:
- Read the audio signal coming from the 3.5mm input jack
- Process the signal, do something basic like maybe delay effect
- Send it to the output jack
do these processes without relying on any or not too much libraries.
Is it too much to handle for a beginner in this subject ? Open to any suggestions, thanks by advance
https://redd.it/1b5oxdh
@r_cpp
Binary tree preorder/inorder/postorder wrong results
If I insert these numbers 42_45_52_55_65_77_79_87_89_99 into the binary tree,
the preorder/inorder/postorder order should be like:
inorder: 42_45_52_55_65_77_79_87_89_99
preorder: 77_45_42_55_52_65_87_79_89_99
postorder: 42_52_65_55_45_79_99_89_87_77
but it turned out to be:
inorder: 42_45_52_55_65_77_79_87_89_99
preorder: 42_45_52_55_65_77_79_87_89_99
postorder: 99_89_87_79_77_65_55_52_45_42
the code:
struct BSTreeNode
{
struct BSTreeNode leftchild;
int data;
struct BSTreeNode rightchild;
};
struct BSTreeNode root;
String tree = "";
struct BSTreeNode newNode(int x)
{
struct BSTreeNode node = new struct BSTreeNode;
node->data = x;
node->leftchild = NULL;
node->rightchild = NULL;
return node;
}
struct BSTreeNode insertBSTree(struct BSTreeNode node , int x)
{ if(node == NULL) return newNode(x);
if(x < node->data)
node->leftchild = insertBSTree(node->leftchild, x);
else
node->rightchild = insertBSTree(node->rightchild, x);
return node;
}
void printBSTree(struct BSTreeNode node)
{ if(node != NULL)
{ printBSTree(node->leftchild);
tree += IntToStr(node->data)+"_";
printBSTree(node->rightchild);
}
}
Button to insert number into the binary tree
void fastcall TForm1::Button1Click(TObject Sender)
{ int data;data = Edit1->Text.ToInt();
root = insertBSTree(root, data);tree = "";
printBSTree(root);
Memo1->Lines->Add(tree);
}
preorder / inorder / postorder code
void preorder(struct BSTreeNode node)
{ if (node != NULL)
{ tree += IntToStr(node->data)+"";
preorder(node->leftchild);
preorder(node->rightchild);
}
}
void inorder(struct BSTreeNode *node)
{ if(node != NULL)
{ inorder(node->leftchild);
tree += IntToStr(node->data)+"\";
inorder(node->rightchild);
}
}
void postorder(struct BSTreeNode node)
{ if(node != NULL)
{ postorder(node->leftchild);
postorder(node->rightchild);
tree += IntToStr(node->data)+"_";
}
}
buttons to execute preorder / inorder / postorder
void __fastcall TForm1::Button5Click(TObject Sender) // preorder
{
tree = "";
preorder(root);
Memo1->Lines->Add(tree);
}
//---------------------------------------------------------------------------
void fastcall TForm1::Button6Click(TObject Sender) // inorder
{
tree = "";
inorder(root);
Memo1->Lines->Add(tree);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button7Click(TObject Sender) // postorder
{
tree = "";
postorder(root);
Memo1->Lines->Add(tree);
}
​
​
https://redd.it/1b5ldk4
@r_cpp
[C++20][performance] Optimized enum_to_name and name_to_enum with no bloat
Some enum_to_name and name_to_enum optimizations using reflection with https://github.com/boost-ext/reflect.
The main goal is to remove the bloat with long function names and improve the run-time whilst having fast compilation-times.
Below (I know it's a lot, tried to make it as simple as possible) is the code which can generate optimized code for converting enum to name and name to enum by using compile-time evaluation of names and run-time execution with 'best' chosen policy. For name to enum it uses minimal perfect hash which is optimized for x86.bmi2 (pext) - more here - https://github.com/boost-ext/mph.
Can that be further optimized? For sure, it's an example of what can be done, with relatively little effort, for policies and perf numbers that always will need measuring in specific use case; there is no silver bullet for perf.
> enum_to_name - can generate if-else/jump-table/switch-case based on enum values and removes the bloat (no function_name... strings)
template<class E, fixed_string unknown = ""> requires std::is_enum_v<E>
[[nodiscard]] constexpr auto enum_name(const E e) noexcept -> std::string_view {
// 1. [compile-time] parse the output of all enum names 'A, B, (E)2, E(3), C, ...'
constexpr auto storage_infos = []<auto... Ns>(std::index_sequence<Ns...>) {
constexpr auto fn_name = detail::function_name<static_cast<E>(Ns)...>(); // get all names at once for faster compilation times
constexpr auto names = fn_name.substr(detail::enum_name_info::begin,
std::size(fn_name)-detail::enum_name_info::end-detail::enum_name_info::begin); // compiler agnostic prefix/postfix
static_vector<char, std::size(names)> storage{};
static_vector<detail::enum_info> infos{};
// parse logic.. (see godbolt)
return std::pair{storage, infos}; // storage is NAME1NAME2NAME3NAME4...
}(std::make_index_sequence<REFLECT_ENUM_MAX>{});
if constexpr (constexpr auto infos = storage_infos.second; std::size(infos) > 0u) {
constexpr auto min = infos[0].index;
constexpr auto max = infos[std::size(infos)-1].index;
constexpr auto is_valid_range = [](const auto value) {
return value >= min and value <= max;
};
const auto value = detail::to_underlying(e);
// 2. [run-time] check that enum is in valid range
if (not is_valid_range(value)) [[unlikely]] {
return unknown;
}
// 3. [compile-time->run-time] based on the inifo generate either if-else/jump-table/switch-case
if constexpr (std::size(infos) < 4u) { // if else
static constinit const auto buffer = storage_infos.first;
for (auto i = 0; i < std::size(infos); ++i) {
if (infos[i].index == value) {
return std::string_view{&buffer[infos[i].begin], infos[i].size};
}
}
} else if constexpr (max-min < 16u) { // jump table
static constinit const auto buffer = storage_infos.first;
constexpr auto find = [infos]<auto I>() -> decltype(auto) {
for (auto i = 0; i < std::size(infos); ++i) { if (I == infos[i].index) { return infos[i]; } }
return detail::enum_info{};
};
constexpr auto jump_table = [&]<auto... Is>(std::index_sequence<Is...>) {
struct alignas(REFLECT_CACHE_LINE_SIZE) : std::array<std::pair<std::uint8_t, std::uint8_t>, sizeof...(Is)> { } array {
[&]() -> std::pair<std::uint8_t, std::uint8_t> {
if constexpr (constexpr auto info = find.template operator()<Is+min>(); info.size) {
return {info.begin, info.size};
} else {
return {};
}
}()...
};
return array;
}(std::make_index_sequence<max-min+1>{});
const auto [begin, size] = jump_table[value-min];
return size ? std::string_view{&buffer[begin], size} : unknown;
}
My late discovery of std::filesystem - Part I
https://www.sandordargo.com/blog/2024/02/28/std-filesystem-part1-paths-and-operations
https://redd.it/1b5eusm
@r_cpp
Has anyone created constexpr versions of cmath functions?
I am thinking of recreating the standard cmath library function as constexpr so you can evaluate things like sin(std::pi)
as a constexpr variable and push the computation at compile-time. I just wanted to know if someone else have done this?
https://redd.it/1b59s27
@r_cpp
Introducing `simple_enum`: A New Approach to enum name computation in C++
Greetings,
I am excited to introduce [simple\_enum](https://github.com/arturbac/simple_enum), a project born from the need for more efficient enumeration name handling in C++. My objective was to explore and demonstrate that enums can indeed be more efficiently managed, with meta-programming techniques, when they are bounded.
**Project Overview:** Conventional high level enum\_name meta-programming frequently grapples with computational inefficiencies. `simple_enum` addresses these by showcasing that, through bounded enumerations, we can achieve not only rapid instantiation but also ensure computational costs are directly proportional to the number of enumerations present.
**Key Advantages:**
* **Zero Runtime Cost**: Achieving `enum_name` for runtime variables without incurring runtime overhead.
* **Compile-time Optimization**: By leveraging the `bounded_enum` concept for enum types, compile-time computation is effectively limited to the number of elements within a range.
* **Enhanced Efficiency**: Through thoughtful optimizations, `simple_enum` minimizes processing loops and optimizes substring parsing, streamlining the overall operation.
**Current Development Phase:** The simple\_enum project is currently undergoing active development. It has demonstrated promising results in preliminary testing across multiple compilers, including Clang 17, GCC 10, GCC 13, and MSVC. Despite all unit tests performing as expected, I consider the project to still be in the development stage. Within the next week, plans are in place to integrate simple\_enum into a corporate project. Assuming this integration proceeds smoothly, I anticipate releasing version 1.0.0 in the next one to two weeks.
**Learn More:** I encourage You to visit my GitHub page for more information, to try out `simple_enum`. [simple\_enum on GitHub](https://github.com/arturbac/simple_enum)
https://redd.it/1b53ify
@r_cpp
a [2017 Raymond Chen](https://devblogs.microsoft.com/oldnewthing/20170301-00/?p=95615) post that discusses EXACTLY this behavior. He implies it is user error. Therefore I'm inclined to boldly, and perhaps wrongly, call this is an `SRW` bug.
What do y'all think?
https://redd.it/1b55686
@r_cpp
Is CMake the de facto standard mandatory to use?
For the past 5 hours I have been banging my head on the table trying to set up a project with CMake. I am really sick of it, I don’t want to use it but it seems like I have to because it’s the de facto standard. Is this really true? Is it possible to become a good developer and eventually land a job without learning and using CMake?
https://redd.it/1b53rks
@r_cpp
LLVM's 'RFC: C++ Buffer Hardening' at Google
https://bughunters.google.com/blog/6368559657254912/llvm-s-rfc-c-buffer-hardening-at-google
https://redd.it/1b6zxee
@r_cpp
What we learned from C++ atomics and memory model standardization - Hans-J. Boehm - The Future of Weak Memory (FOWM) 2024
https://www.youtube.com/watch?v=Ss7gIs2-hzk
https://redd.it/1b6fu24
@r_cpp
Is shadowing a member variable from a base class a bad thing? Maybe, but maybe not
https://devblogs.microsoft.com/oldnewthing/20240304-00/?p=109472
https://redd.it/1b6hv6p
@r_cpp
The Compositor Modules library to easily create Wayland compositors officially announced; uses modern C++/CMake techniques
https://www.phoronix.com/review/the-compositor-modules-como
https://redd.it/1b6gixw
@r_cpp
Build C++ projects for multiple OS in GitHub or Azure Pipelines
Often I get asked by customers how they can easily build their C++ projects for a different combination of API versions and operating systems. I decided to provide a real generic approach which can be used for various C++ projects, it is called cpp-multi-builder.
Features are:
\- Easy setup with minimal configuration, example included.
\- Support for multiple operating systems.
\- Integrated Conan package manager for easy C++ dependency management.
\- Automated testing and packaging, plus debug files.
\- Caching support for GitHub actions.
\- Compatibility with both GitHub Actions and Azure Pipelines.
https://github.com/andygruber/cpp-multi-builder
I am thinking about adding gitlab support too.
I am no C++ expert, so if I made some mistakes there, would be glad to know and fix it. But I know a lot of C++ developers in my area are struggling with this kind of automation/CMake/different-OS-builds. But it may be my area as well, who knows.
Any general thoughts about it? Is it useful? Any features missing? Other feedback? thx in advance.
https://redd.it/1b6datl
@r_cpp
[boxed-cpp] C++ primitive type boxing
[boxed-cpp](https://github.com/contour-terminal/boxed-cpp) is a small header-only library for easing primitive type boxing in C++. Primary goal of the library is to make it easy to avoid code with easily swappable parameters [clang-tidy:bugprone-easily-swappable-parameters](https://clang.llvm.org/extra/clang-tidy/checks/bugprone/easily-swappable-parameters.html).
Library provides an easy way to create unique types and use them
using rho_type = boxed::boxed<double>;
using phi_type = boxed::boxed<double>;
using theta_type = boxed::boxed<double>;
double x_coord(rho_type, phi_type, theta_type);
to call `x_coord` function you need to specify types explicitly
x_coord(rho_type{1.0}, phi_type{0.3}, theta_type{0.75});
This allows some fun functionality with adjusting the order of parameters at compile time, as following:
using rho_type = boxed::boxed<double>;
using theta_type = boxed::boxed<double>;
using phi_type = boxed::boxed<double>;
template <typename Func, typename... Tuple> struct Wrap_with_tuple {
using type_order = std::tuple<Tuple...>;
Wrap_with_tuple(Func f, type_order s) : _func(f), _order(s) {};
template <typename... F> decltype(auto) operator()(F... args) {
auto arg_tuple = std::make_tuple(args...);
auto ints = std::make_index_sequence<sizeof...(args)>{};
return make_call(arg_tuple, ints);
}
template <typename call_tuple, typename T, T... ints>
decltype(auto) make_call(call_tuple arg_tuple,
std::integer_sequence<T, ints...> int_seq) {
return _func(
std::get<std::decay_t<decltype(std::get<ints>(_order))>>(arg_tuple)...);
}
Func _func;
type_order _order;
};
auto x_coord = Wrap_with_tuple(
[](rho_type rho, theta_type theta, phi_type phi) {
return unbox(rho) * sin(unbox(theta)) * cos(unbox(phi));
},
std::make_tuple(rho_type{}, theta_type{}, phi_type{}));
int main(){
rho_type r{1.0};
theta_type th{0.5};
phi_type ph{0.75};
assert(x_coord(r,th,ph) == x_coord(ph,th,r));
}
To use this library you need c++20 support and most recent release of compiler see [https://godbolt.org/z/EbMPbsK7v](https://godbolt.org/z/EbMPbsK7v) If you want to use library with older compiler, you have to specify unique tags yourself for each boxed type
struct Tag{};
using boxed_type = boxed::boxed<int,Tag>;
https://redd.it/1b65nt1
@r_cpp
Videos like the median problem solving video by Pete Isensee at CppCon 2023
Really enjoyed the way the presenter showed a step-by-step approach to building a median algorithm and I am now excited to watch more such videos. Need not be exactly in the same manner but preferably where the presented is using C++ to solve the problem and improve the solution. Please post your suggestions even if it is not a video but are blogs or books. Would love to check them out.
Link: https://youtu.be/izxuLq_HZHA?si=2lWkCFUnzmgX6cSF
https://redd.it/1b5rksi
@r_cpp
The comma operator in the for loop does not allow multiple variables to be declared.
// Compiles
string::const_reverse_iterator S1_crit;
string::const_reverse_iterator S2_crit;
for (S1_crit = strS1.rbegin(), S2_crit = strS2.rbegin();
S1_crit != S1_crend || S2_crit != S2_crend;)
// Does not compile
for (string::const_reverse_iterator S1_crit = strS1.rbegin(),
string::const_reverse_iterator S2_crit = strS2.rbegin();
S1_crit != S1_crend || S2_crit != S2_crend;)
https://redd.it/1b5p35w
@r_cpp
else { // switch case
static constinit const auto buffer = storage_infos.first;
const auto switch_case = [&]<auto I = 0>(auto switch_case, const auto value) -> std::string_view {
switch (value) {
default: {
if constexpr (I < max) {
return switch_case.template operator()<I+1>(switch_case, value);
}
}
case infos[I].index: return std::string_view{&buffer[infos[I].begin], infos[I].size};
}
detail::unreachable();
};
return switch_case(switch_case, value);
}
}
return unknown;
}
For example for enum `enum class e_long {_1, _2, _3, _4, _5, _6, _7, _8, _9, _10};` the .text will be `.ascii
"_1_2_3_4_5_6_7_8_9_10".
> name_to_enum - optimized with minimal perfect hash (used https://github.com/boost-ext/mph) which can generate code optimized for bmi2 with pext with generated at compile-time masks
template<class E> requires std::is_enum_v<E>
[[nodiscard]] constexpr auto name_to_enum(const auto name) noexcept -> E {
// 1. [compile-time] parse the output of all enum names 'A, B, (E)2, E(3), C, ...'
constexpr auto storage_infos = ...; // same as in enum_to_name
if constexpr (constexpr auto infos = storage_infos.second; std::size(infos) > 0u) {
// 2. [compile-time] create constexpr array with {enum_name, value} pairs
constexpr auto names = [&]<auto... Is>(std::index_sequence<Is...>) {
return std::array{std::pair{
mph::fixed_string{&storage_infos.first[infos[Is].begin], infos[Is].size},
static_cast<E>(infos[Is].index)}...
};
}(std::make_index_sequence<std::size(infos)>{});
// 3. [compile-time->run-time] Use mph to generate minimal perfect hash table (policy bmi2.pext)
return *mph::hash<names>(name);
}
return {};
}
> To see different assembly
enum class e_short {_1, _2, _3};
enum class e_long {_1, _2, _3, _4, _5, _6, _7, _8, _9, _10};
enum class e_long_sparse {_1=10, _2=20, _3=30, _4=40, _5=50, _6=60, _7=70, _8=80, _9=90, _10=100};
// Uncomment to see how the generated assembly is changing
//template auto reflect::enum_name(e_short) -> std::string_view; // if-else
//template auto reflect::enum_name(e_long) -> std::string_view; // jump-table
//template auto reflect::enum_name(e_long_sparse) -> std::string_view; // switch-case
//template auto reflect::name_to_enum<e_short>(std::string_view) -> e_short; // mph.policies.pext
//template auto reflect::name_to_enum<e_long>(std::string_view) -> e_long; // mph.policies.pext
//template auto reflect::name_to_enum<e_long_sparse>(std::string_view) -> e_long_sparse; // mph.policies.pext
Full example -> https://godbolt.org/z/5E8zqc3c5
Updates -> https://twitter.com/krisjusiak/status/1764278301210624085
https://redd.it/1b5h46b
@r_cpp
Encryption and Decryption using Linear Algebra with C++
https://github.com/farukalpay/TextEncryptionWithLinearAlgebra
https://redd.it/1b5fph4
@r_cpp
Start your C++ project today!
https://www.youtube.com/watch?v=kmst4j7CB9M
https://redd.it/1b5db2p
@r_cpp
Difference between Classes and Functions
I've started programming just a few days ago and M now confused between class and function
Help!
https://redd.it/1b5a69v
@r_cpp
Speed of GUI libraries in C++
How do different GUI libraries compare speedwise? For example, I read that wxWidgets wraps native functionality with its own API. Does that mean calls to wxWdigets go through an extra layer of indirection, compared to, say Win32? Also, when Qt and GTK do their own rendering, is that better or worse than wxWidgets? What is the main factor affecting speed in GUI libraries?
Note: I'm not developing a game. I'm just a CS college student curious about the subject of speed and GUI libraries.
https://redd.it/1b55jpg
@r_cpp
Maybe possible bug in std::shared_mutex on Windows
A team at my company ran into a peculiar and unexpected behavior with `std::shared_mutex`. This behavior only occurs on Windows w/ MSVC. It does not occur with MinGW or on other platforms.
At this point the behavior is pretty well understood. The question isn't "how to work around this". The questions are:
1. Is this a bug in `std::shared_mutex`?
2. Is this a bug in the [Windows SlimReaderWriter](https://learn.microsoft.com/en-us/windows/win32/sync/slim-reader-writer--srw--locks) implementation?
I'm going to boldly claim "definitely yes" and "yes, or the SRW behavior needs to be documented". Your reaction is surely "it's never a bug, it's always user error". I appreciate that sentiment. Please hold that thought for just a minute and read on.
Here's the scenario:
1. Main thread acquires **exclusive** lock
2. Main thread creates N child threads
3. Each child thread:
1. Acquires a **shared** lock
2. Yields until all children have acquired a shared lock
3. Releases the **shared** lock
4. Main thread releases the **exclusive** lock
This works ***most*** of the time. However 1 out of \~1000 times it "deadlocks". When it deadlocks exactly 1 child successfully acquires a shared lock and all other children block forever in `lock_shared()`. This behavior can be observed with `std::shared_mutex`, `std::shared_lock`/`std::unique_lock`, or simply calling `SRW` functions directly.
If the single child that succeeds calls `unlock_shared()` then the other children will wake up. However if we're waiting for all readers to acquire their shared lock then we will wait forever. Yes, we could achieve this behavior in other ways, that's not the question.
I made a [StackOverflow post](https://stackoverflow.com/questions/78090862/stdshared-mutexunlock-shared-blocks-even-though-there-are-no-active-exclus) that has had some good discussion. The behavior has been confirmed. However at this point we need a language lawyer, u/STL, or quite honestly Raymond Chen to declare whether this is "by design" or a bug.
Here is code that can be trivially compiled to repro the error.
```cpp
#include <atomic>
#include <cstdint>
#include <iostream>
#include <memory>
#include <shared_mutex>
#include <thread>
#include <vector>
struct ThreadTestData {
int32_t numThreads = 0;
std::shared_mutex sharedMutex = {};
std::atomic<int32_t> readCounter;
};
int DoStuff(ThreadTestData* data) {
// Acquire reader lock
data->sharedMutex.lock_shared();
// wait until all read threads have acquired their shared lock
data->readCounter.fetch_add(1);
while (data->readCounter.load() != data->numThreads) {
std::this_thread::yield();
}
// Release reader lock
data->sharedMutex.unlock_shared();
return 0;
}
int main() {
int count = 0;
while (true) {
ThreadTestData data = {};
data.numThreads = 5;
// Acquire write lock
data.sharedMutex.lock();
// Create N threads
std::vector<std::unique_ptr<std::thread>> readerThreads;
readerThreads.reserve(data.numThreads);
for (int i = 0; i < data.numThreads; ++i) {
readerThreads.emplace_back(std::make_unique<std::thread>(DoStuff, &data));
}
// Release write lock
data.sharedMutex.unlock();
// Wait for all readers to succeed
for (auto& thread : readerThreads) {
thread->join();
}
// Cleanup
readerThreads.clear();
// Spew so we can tell when it's deadlocked
count += 1;
std::cout << count << std::endl;
}
return 0;
}
```
Personally I don't think the function `lock_shared()` should ever be allowed to block forever when there is not an exclusive lock. That, to me, is a bug. One that only appears for `std::shared_mutex` in the `SRW`\-based Windows MSVC implementation. *Maybe* it's allowed by the language spec? I'm not a language lawyer.
I'm also inclined to call the `SRW` behavior either a bug or something that should be documented. There's
Module partition implémentation issue
Hello. I am trying to use c++ 20 modules in a personal project. My build system is a simple make file with .d files emission (build system part is ok and I’d like to keep it simple)
Right now I have it working fine with modules as follow :
I have a main_module.cppm that first export import interfaces partitions and then export import implementations partitions.
My interfaces only import interfaces. But the issue is that my implementation must import other implementation else I get linkage error.
I tried to declare as follow :
——
Interface :
Export module name:partname
…
Implementation :
Module name:partname
…
——
But with this my implementation is unable to find initial interfaces. It causes problems if I have classes members functions that I try to implement. It does work with regular functions.
So I endeded up with this mess that I want to replace :
Interfaces :
Export module name:partname
…
Implementation :
Export module name:partname.impl
Export import :partname
Import :another part.impl
…
——
The root problem I am trying to solve is that I don’t want to recompile all partitions that depends on an interface when I change its implementation.
I’m using llvm 17 (I’m waiting for llvm 18 to check if any change about that, It should be released in few days)
Thanks for your reading !
https://redd.it/1b4yga8
@r_cpp