logo
down
shadow

is it safe to use the same mutex with lock_gard and without it in other parts of code


is it safe to use the same mutex with lock_gard and without it in other parts of code

By : dschurma
Date : January 11 2021, 05:14 PM
will help you Yes, you can effectively mix and match different guard instances (e.g. lock_guard, unique_lock, etc...) with std::mutex in different functions. One case I run into occassionally is when I want to use std::lock_guard for most methods, but usage of std::condition_variable expects a std::unique_lock for its wait method.
To elaborate on what Oblivion said, I typically introduce a new scope block within a function so that usage of std::lock_guard is consistent. Example:
code :
void func2() {

    {  // ENTER LOCK
        lock_guard<std::mutex> lck;

       //some code that should not be executed when func1 is executed

    } // EXIT LOCK

    // some other (thread safe) code 
}


Share : facebook icon twitter icon
Is mutex+atomic necessary to make this code thread safe, or is mutex enough?

Is mutex+atomic necessary to make this code thread safe, or is mutex enough?


By : user3244609
Date : March 29 2020, 07:55 AM
seems to work fine You do not need to use atomic variables if all accesses to those variables are protected by a mutex. This is the case in your code, as all public member functions lock addMutex on entry. Therefore addIndex can be a plain int and everything will still work fine. The mutex locking and unlocking ensures that the correct values become visible to other threads in the right order.
std::atomic<> allows concurrent access outside the protection of a mutex, ensuring that threads see correct values of the variable, even in the face of concurrent modifications. If you stick to the default memory ordering it also ensures that each thread reads the latest value of the variable. std::atomic<> can be used to write thread-safe algorithms without mutexes, but is not required if all accesses are protected by the same mutex.
Is `mem::swap`ing a `Mutex<T>` really safe?

Is `mem::swap`ing a `Mutex<T>` really safe?


By : Rana El-Shenaity
Date : March 29 2020, 07:55 AM
will be helpful for those in need Mutex is Sync, which means it's allowed for multiple threads to access the same object concurrently. Types that make the sharing of a single object across threads possible in the first place (Arc, but also global variables) need to make sure that the objects they share are Sync to ensure integrity.
However, that doesn't avoid Rust's other aliasing rules. Specifically, only one mut borrow of a variable can exist at any given time, and its existence must prevent any other access to the variable. The things that make sharing possible must ensure this, as well. Arc does this by simply never handing out mut references to its pointee; you can only get non-mut references. This is why Mutex::lock takes &self: it must be possible to call it on a non-mut reference. Similarly, Atomic*::store and all other manipulation methods take &self. static mut variables, on the other hand, simply can only be accessed within unsafe code, where the programmer is responsible for upholding guarantees.
Is this Go code thread safe or do I need a mutex?

Is this Go code thread safe or do I need a mutex?


By : Phan Luân
Date : March 29 2020, 07:55 AM
it helps some times The compiler may not reorder the assignment and close statement, so you do not need a mutex if callers are well-behaved and do as instructed by your docs.
This is explained in The Go Memory Model, Channel Communication.
Is it safe to use a Mutex without Arc in multithreading?

Is it safe to use a Mutex without Arc in multithreading?


By : user2096254
Date : March 29 2020, 07:55 AM
may help you . This is safe, yes. Multithreading is exactly what mutexes are for. Arc doesn't give you anything you need here.
Note, of course, that your current code is horrendously inefficient. I hope your real use case does a lot more work between mutex locks than this.
C++ Meyers Singleton - thread safe (code equivalent for mutex?)

C++ Meyers Singleton - thread safe (code equivalent for mutex?)


By : Sunish Singhal
Date : March 29 2020, 07:55 AM
will be helpful for those in need In C++11 (which is implied by std::mutex) the whole Meyers singleton is expressed in two lines:
Related Posts Related Posts :
  • Two template classes use each other as template argument
  • Why am I not getting any output, for my code on insertion in linked list?
  • What is the correct way of implementing this custom priority_queue
  • Unable to set the location for input in vertex shader
  • Qt: How to Access Inherited Widget?
  • Why same char is not equal
  • Why does using a range for loop gives different output than using a regular for loop in this scenario?
  • Binary tree coding problems with c++?
  • How to safely change the type of a pointer
  • Could this publish / check-for-update class for a single writer + reader use memory_order_relaxed or acquire/release for
  • Passing a function identifier as an rvalue reference and applying std::move() to it
  • The conditional operator is not allowing the program to terminate
  • Define a c++ string as "\"
  • memcpy on __declspec naked returns unexpected bytes
  • What is the proper way to link enums with CMake?
  • How to decode MAP Invoke messages using asn1c generated code
  • How do you write multiple lines in a .txt with recursion?
  • Member function with strange type causing callback function mismatch
  • Visual Studio optimisations break SDL graphical output
  • How to use less memory in Sieve_of_Eratosthenes
  • Covariance in Callback Parameters C++
  • switch may fall through (no it may not)
  • Compilation fails calling Cocoa function from C++
  • How to handle classes with differently named member functions in algorithms?
  • Convert QString to QJsonArray
  • Data exchange finished in CPropertyPage::OnOK?
  • Template member specialization in template class
  • Is it not possible to assign a struct to an index of a vector?
  • Why is empty unordered_map.find not returning end()?
  • Template argument deduction for inheriting specializations
  • dlopen undefined reference
  • Member function of class with template arguments and default arguments outside class
  • Is it possible to implement a non-owning "slightly smart" pointer on top of standard weak pointers?
  • how to configure the AcquireCredentialsHandleA correctly
  • Using private versions of global extern variables with OpenMP
  • Eigen Block wrong amount of columns and rows
  • Memory alignment rules in inheritance
  • Is nullptr falsy?
  • tm_wday returns a large integer outside 0-6 range
  • Scope a using declaration, inside a header
  • How to specify constructor's template arguments inside a new expression?
  • Avoid allocating in constructor or preserve simplicity (and RAII?)
  • Can C++ raise an error when std array initialization is too small?
  • Reference to end is ambiguous
  • Problem with basic usage of std::enable_if
  • How to print out a decimal number as octal number using setiosflags in C++
  • Open Visual Studio with solution and specific file and line
  • Enum value based on template type
  • Is there any way to swap nodes in std::list?
  • How to copy QString into wchar_t buffer
  • Make the compiler generate an empty default function for an std::function
  • Insert or push_back to end of a std::vector?
  • Best practice for const temporary types
  • Include CSV File in Complied Binary
  • Issue with binding non static function to callback
  • How can read from some files and write content of them with different form into files with same names
  • Why does auto deduce this variable as double and not float?
  • no instance of overloaded function "std::make_unique" matches the argument list, but works with unique_ptr con
  • How to see array size from a pointer in c++
  • Error taking address of temporary in Eclipse
  • shadow
    Privacy Policy - Terms - Contact Us © festivalmusicasacra.org