lundi 16 octobre 2023

Why standard doesn't require std::mutex::~mutex synchronizes-with with the latest unlock

struct X
{
    std::mutex m;
    std::string str;
    
    void set(std::string s) 
    {
        auto _ = std::unique_lock(m);
        str = std::move(s);
    }
    
    ~X()
    {
        // auto _ = std::unique_lock(m);
    }
}

Is there any part of the standard that guarantees that ~X will never experience race conditions inside ~string without the commented line?

The exclusive access to the object and/or lifetime could be managed by an atomic variable with RELAXED semantics (== no other data except the fact of the end of lifetime is synched). We have a mutex to protect the access to the object, so using relaxed operation seems to be ok to synchronize exclusive access/lifetime and mutex to access the data.

If the standard would require ~mutex to synchronize with the latest unlock and if we moved the declaration of the mutex below the protected data then we could afford default ~X but without this requirement, we need always have explicit destructor which locks all the mutexes used to protect any member.

PS to help understand my question use this example https://en.cppreference.com/w/cpp/thread/mutex there is a comment that accessing g_pages without a lock is safe. Why, which part of the standard guarantees this? The fact that both threads are joined only guarantees no multi-threaded access to the map, but it doesn't guarantee synchronize-with relationship with the latest mutex::unlock operation. I run many different programs trying to expose race conditions and I am pretty sure that mutex uses std::atomic_thread_fence for both lock and unlock which synchronizes with thread.join that must use at least some atomic to work. But the problem is standard doesn't require mutex to use std::atomic_thread_fence and it it just happens that all known to me implementations use it.

#include <chrono>
#include <iostream>
#include <map>
#include <mutex>
#include <string>
#include <thread>
 
std::map<std::string, std::string> g_pages;
std::mutex g_pages_mutex;
 
void save_page(const std::string &url)
{
    // simulate a long page fetch
    std::this_thread::sleep_for(std::chrono::seconds(2));
    std::string result = "fake content";
 
    std::lock_guard<std::mutex> guard(g_pages_mutex);
    g_pages[url] = result;
}
 
int main() 
{
    std::thread t1(save_page, "http://foo");
    std::thread t2(save_page, "http://bar");
    t1.join();
    t2.join();
 
    // safe to access g_pages without lock now, as the threads are joined
    for (const auto &pair : g_pages)
        std::cout << pair.first << " => " << pair.second << '\n';
}

What is the efficient difference between insert and emplace? [duplicate]

What is the efficient difference between belows?

std::string a = "a";
std::string b = "b";

std::map<std::string, std::string> m;
m.insert({a,b});  -->1
m.insert(make_pair{a,b});  -->2
m.emplace(a,b);  -->3

dimanche 15 octobre 2023

What do I do when I am unable to resolve the configuration for gcc?

What do you do when the following error pops up; {Unable to resolve configuration with compilerPath: "gcc"}

I have tried looking the error up and it doesn't really help me out. My code: your text#include your textusing namespace std; your textcout<<"hello"<<endl;

samedi 14 octobre 2023

Move semantics with 2d array in C++

Can someone please explain me how to make move semantics with 2d array of floats in a right way? I tried something like that:

struct Matrix {
    Matrix& operator=(Matrix&& rhs) noexcept {
        if (this == &rhs) {
            return *this;
        }

        for (size_t i = 0; i < rows; ++i) {
            delete[] matrix[i];
        }

        delete[] matrix;
        matrix = rhs.matrix;

        for (size_t i = 0; i < rows; ++i) {
            matrix[i] = rhs.matrix[i];
            rhs.matrix[i] = nullptr;
        }

        rhs.matrix = nullptr;
        return *this;
    }

    ~Matrix() {
        for (size_t i = 0; i < rows; ++i) {
            delete[] matrix[i];
        }

        delete[] matrix;
    }

    int rows;
    int cols;
    float** matrix;
};

but in a line rhs.matrix = nullptr matrix became nullptr and I don't know how to fix it.

I tried to just change pointers in inner arrays but outer pointer leave with no changes. But I think it is wrong way because programm logic became incorrect.

error: expected ';' at end of member declaration, error: redeclaration of 'int Printer::s1', error: expected ';' after class definition

#include #include

using std::cout; using std::endl;

class Printer { private: char *s1; public: char *SetString(s1); void Showstring(); }; char Printer::*SetString(s1){ return s1; } void Printer::Showstring(){ cout<<SetString(s1); } int main(void){ Printer pnt; pnt.SetString("Hello"); pnt.Showstring(); }

'pnt.SetString("Hello");' save "Hello" to 'private: char *s1;' 'pnt.Showstring();' bring 'private: char *s1;' and print s1

How to get color settings of system UI (color theme) in Windows 10 with Python?

I'm trying to get some color settings in UI of Windows 10, e.g. colors of window title bar when windows are activated/inactivated, the edge color of window. Or to be more simple, I'm searching for a function like getSysColor() in winuser.h(Documentation) in Windows 10.

What I mean by "colors of window title bar when windows are activated" under "color settings in UI of Windows 10". (red circle)

Here's something I've tried:

  1. getSysColor(nIndex) in winuser.h:

    This function cannot work well under Windows 10, for some keys(nIndex) are not supported according to the (Documentation) . For example, if I ask for the colors of activated window title bar with ctypes.windll.user32.GetSysColor(2) in Python, it will return the color of ##99B4D1, which is maybe the color under Windows 7, rather than the ##0078D7 in Windows 10. But I don't want a color set of Windows 7.

  2. DwmGetColorizationColor() in dwmapi.h:

    Some suggest that this function may retrieve the color of title bar. But I can only get a strange yellow (##F8FFA9) with ctypes.windll.dwmapi.DwmGetColorizationColor(), which is definetly not what I want. Meanwhile this function can only offer color of title bar, rather than all the color used in Windows UI.

Trivially Copyable types - do all elligible methods need to be trivial?

The cppreference page for the named requirement of being TriviallyCopyable, in the subsection for classes, lists the following requirement:

Trivially copyable class

A trivially copyable class is a class that

  • has at least one eligible copy constructor, move constructor, copy assignment operator, or move assignment operator,
  • each eligible copy constructor is trivial
  • each eligible move constructor is trivial
  • each eligible copy assignment operator is trivial
  • each eligible move assignment operator is trivial, and
  • has a non-deleted trivial destructor.

so, if there's a trivial copy ctor but a non-trivial, say, move assignment operator - isn't the class trivially copyable? My intuition says that if you can construct an object of the same type by trivially copying the existing object - that's trivial copyability.