stdmutex

Why did CRITICAL_SECTION performance become worse on Win8

ⅰ亾dé卋堺 提交于 2020-12-30 09:47:37
问题 It seems like CRITICAL_SECTION performance became worse on Windows 8 and higher. (see graphs below) The test is pretty simple: some concurrent threads do 3 million locks each to access a variable exclusively. You can find the C++ program at the bottom of the question. I run the test on Windows Vista, Windows 7, Windows 8, Windows 10 (x64, VMWare, Intel Core i7-2600 3.40GHz). The results are on the image below. The X-axis is the number of concurrent threads. The Y-axis is the elapsed time in

Is a copy-on-return operation executed prior or after lock_guard destructor? [duplicate]

 ̄綄美尐妖づ 提交于 2020-07-15 04:36:48
问题 This question already has answers here : C++ return value created before or after auto var destruction? (2 answers) in C++ which happens first, the copy of a return object or local object's destructors? [duplicate] (4 answers) Closed 2 years ago . Is the get_a() function safe for race-conditions or do I need to explicitly copy str_ as in get_b() in order to have a thread-safe function? class Class { public: auto get_a() -> std::string { auto&& guard = std::lock_guard{mutex_}; return str_; }

Using std::mutex as member variable in a class

为君一笑 提交于 2020-03-03 08:47:05
问题 I have defined a class that has std::mutex my_mutex as its private member variable. But when I try to use it using lock_guard in a member function which is called from different threads, the compiler throws up a lots of errors. If I keep this mutex outside the class it works. The code is as follows class ThreadClass { std::mutex my_mutex; public: void addToList(int max, int interval) { std::lock_guard<std::mutex> guard(my_mutex); for (int i = 0; i < max; i++) { // Some operation } } }; int

Why is std::mutex taking a long, highly irregular amount of time to be shared?

被刻印的时光 ゝ 提交于 2019-12-30 11:03:50
问题 This code demonstrates that the mutex is being shared between two threads, but one thread has it nearly all of the time. #include <thread> #include <mutex> #include <iostream> #include <unistd.h> int main () { std::mutex m; std::thread t ([&] () { while (true) { { std::lock_guard <std::mutex> thread_lock (m); sleep (1); // or whatever } std::cerr << "#"; std::cerr.flush (); } }); while (true) { std::lock_guard <std::mutex> main_lock (m); std::cerr << "."; std::cerr.flush (); } } Compiled with

Why do functions using std::mutex make a null check of the address of pthread_key_create?

断了今生、忘了曾经 提交于 2019-12-17 20:19:23
问题 Take this simple function that increments an integer under a lock implemented by std::mutex : #include <mutex> std::mutex m; void inc(int& i) { std::unique_lock<std::mutex> lock(m); i++; } I would expect this (after inlining) to compile in a straightforward way to a call of m.lock() an increment of i and then m.unlock() . Checking the generated assembly for recent versions of gcc and clang , however, we see an extra complication. Taking the gcc version first: inc(int&): mov eax, OFFSET FLAT:_

Different behavior when `std::lock_guard<std::mutex>` object has no name

女生的网名这么多〃 提交于 2019-12-12 10:45:46
问题 I'm learning about std::mutex , std::thread and I am surprised at the different behavior of 2 pieces of code below: #include <iostream> #include <mutex> #include <thread> using namespace std; std::mutex mtx; void foo(int k) { std::lock_guard<std::mutex> lg{ mtx }; for (int i = 0; i < 10; ++i) cout << "This is a test!" << i << endl; cout << "The test " << k << " has been finished." << endl; } int main() { std::thread t1(foo, 1); std::thread t2(foo, 2); t1.join(); t2.join(); return 0; } The

std::mutex in shared memory not working

随声附和 提交于 2019-12-11 04:45:08
问题 I have a scenario where the shared memory area is exclusively accessed by two different processes. When I launch the processes, the first process successfully locks the mutex, updates the memory and unlock the mutex. But I observe that when the second process try to lock it, it is still in deadlock state, waiting for mutex to unlock. Time difference between the mutex lock is 10s for first and second process. I am using the std::mutex. Please tell me what I am missing. 回答1: An std::mutex

Why is std::mutex taking a long, highly irregular amount of time to be shared?

扶醉桌前 提交于 2019-12-01 08:23:44
This code demonstrates that the mutex is being shared between two threads, but one thread has it nearly all of the time. #include <thread> #include <mutex> #include <iostream> #include <unistd.h> int main () { std::mutex m; std::thread t ([&] () { while (true) { { std::lock_guard <std::mutex> thread_lock (m); sleep (1); // or whatever } std::cerr << "#"; std::cerr.flush (); } }); while (true) { std::lock_guard <std::mutex> main_lock (m); std::cerr << "."; std::cerr.flush (); } } Compiled with g++ 7.3.0 on Ubuntu 18.04 4.15.0-23-generic. The output is a mix of both # and . characters, showing