Questions tagged [stdmutex]

44 questions
1
vote
1 answer

c++ threads safety and time efficiency: why does thread with mutex check sometimes works faster than without it?

I'm beginner in threads usage in c++. I've read basics about std::thread and mutex, and it seems I understand the purpose of using mutexes. I decided to check if threads are really so dangerous without mutexes (Well I believe books but prefer to see…
Sergey
  • 13
  • 2
0
votes
2 answers

Is `std::shared_mutex` virtually a superset of `std::mutex`?

std::shared_mutex can provide an exclusive lock like std::mutex. Plus it extends the synchronization capabilities by allowing the parallel read-only accesses to the multiple threads. Under the hood the number of machine instructions might be lesser…
iammilind
  • 68,093
  • 33
  • 169
  • 336
0
votes
1 answer

std::shared_lock release() method - how to use?

Documentation on the method (https://en.cppreference.com/w/cpp/thread/shared_lock/release) sounds confusing to me: disassociates the associated mutex without unlocking (i.e., releasing ownership of) it I used to explore shared_lock and unique_lock…
user3714601
  • 1,156
  • 9
  • 27
0
votes
2 answers

Iterating over vector in one thread while other may potentially change reference

I have a vector allow_list that is periodically updated in a thread while another serves a function that checks if a certain string is in that allow_list via: if (std::find(allow_list->begin(), allow_list->end(), target_string) !=…
Baiqing
  • 1,223
  • 2
  • 9
  • 21
0
votes
1 answer

Horrible scalability of std::mutex with zero contention

Even without contention, the scalability of std::mutex seems to be horrible. This is a case where every thread is guaranteed to use its own mutex. What is going on? #include #include #include void TestThread(bool…
0
votes
4 answers

Destructor, when object's dynamic variable is locked by mutex will not free it?

I'm trying to solve some complicated (for me at least) asynchronous scenario at once, but I think it will be better to understand more simple case. Consider an object, that has allocated memory, carrying by variable: #include #include…
Ngdgvcb
  • 155
  • 6
0
votes
1 answer

destroy std::mutex vs pthread_mutex_t

What is the difference between std::mutex and pthread_mutex_t in CPP? What is the correct way to destroy each one of the mutex types when they are dynamically allocated as follows: pthread_mutex_t * mutex1 = new pthread_mutex_t(); std::mutex *…
0
votes
1 answer

c++ mutexes that not blocks code from inside, but allow to block it from outside

I'm trying to solve the next problem: There are two types of functions. The first type can execute from different threads simultaneously (for example - read data from container). The second one must block all threads inside first-type-function until…
AtremSh
  • 23
  • 1
  • 3
0
votes
0 answers

c++11 std::mutex locking same thread

I am studying (refreshing) C++ now. As part of refreshing, I tried to check std::mutex behavior by me below code. It basically trys to do 'Manual lock/unlock', 'std::lock_guard over mutex' and 'try_lock over std::mutex'. When I refer…
gymk
  • 1
  • 2
0
votes
0 answers

How many mutex(es) should be used in one thread

I am working on a c++ (11) project and on the main thread, I need to check the value of two variables. The value of the two variables will be set by other threads through two different callbacks. I am using two condition variables to notify changes…
0
votes
2 answers

Correctness of using std::atomic in combination with std::mutex

I have written a shared priority queue class. To signal to stop serving data I use method Cancel(), which sets a sign done to false and application is not allowed to write / read any data from the queue. I am not sure about using std::atomic
Dom
  • 532
  • 1
  • 9
  • 23
-1
votes
2 answers

Protecting for loop with std::mutex

I need to protect data container with std::mutex that is used in for loop like this: for (auto it = data.begin(); it != data.end(); ++it) { std::cout << it->param; ... } I can think of several options but they are ugly like this: { //…
Sergey
  • 31
  • 2
-1
votes
3 answers

Is it necessary to lock?

I met a question in leetcode. I have viewed some solutions in the discuss. But my solution is different from others because I do not use the lock in the method first. I wonder whether my code is correct. Besides, can you give me some advice about my…
Alfred
  • 71
  • 5
-2
votes
1 answer

RAII: do mutexes in a vector declared in a loop all unlock in the next iteration?

Suppose I have the following: // ... necessary includes class X { struct wrapper{ std::mutex mut{}; } std::array wrappers{}; void Y() { for (auto i{0u}; i < 10; ++i) { …
SRSR333
  • 187
  • 4
  • 15
1 2
3