Questions tagged [unique-lock]

The class std::unique_lock is a general-purpose mutex ownership wrapper allowing deferred locking, time-constrained attempts at locking, recursive locking, transfer of lock ownership, and use with condition variables.

32 questions
0
votes
1 answer

Trying to write asynchronous I/O in C++ using locks and condition variables. This code calls terminate on the first lock() why?

We were trying to create C++ code that would read a block from a file, and start a thread to asynchronously read the next block while processing the first block. We started with condition_variable, but it was crashing so we went with straight…
Dov
  • 8,000
  • 8
  • 46
  • 75
0
votes
1 answer

What exactlly is lock with adopt_lock_t

When I checked adopt_lock_t, it says adopt_lock_t assume the calling thread already has ownership of the mutex. So what's the meaning of the word assume? What if other thread already holding the mutex when I claim the lock(adopt_lock_t) with the…
f1msch
  • 509
  • 2
  • 12
0
votes
1 answer

Last notify_all isn't triggering last conditional_variable.wait

What I'm Trying To Do Hi, I have two types of threads the main one and the workers where the workers are equal to the number of cores on the CPU, what I'm trying to do is when the main thread needs to call an update I set a boolean called Updating…
0
votes
0 answers

Capture a STD::unique_loc in a lambda function which is used as a thread function

Is there any potential problem if capturing a STD::unique_loc in a lambda function which is used as a thread function? e.g: int foo() { std::mutex mtxRunOver; std::unique_lock ulkRunOver(mtxRunOver); std::thread…
John
  • 2,963
  • 11
  • 33
0
votes
2 answers

Why conditional_variable::notify_all may not wake up any thread?

I use conditional_variable::notify_all() to wake up a waiting thread(only one thread is waiting for the unique_lock indeed). This code snippet works well at most time, but the log files(for details, see below) indicates that the parent thread could…
John
  • 2,963
  • 11
  • 33
0
votes
1 answer

If exception occurred unique lock will unlock the mutex

#include #include #include #include using namespace std; mutex m; int count; void func (const char *des) { std::unique_lock < mutex > ul (m); cout << "in :" << des << endl; try { if (des == "T1") …
Sravani
  • 59
  • 5
0
votes
0 answers

My thread-safe queue code appears to work, any possible race conditions, deadlocks, or other design problems?

I am new to using condition_variables and unique_locks in C++. I am working on creating an event loop that polls two custom event-queues and a "boolean" (see integer acting as boolean), which can be acted upon by multiple sources. I have a demo…
PMARINA
  • 304
  • 4
  • 17
0
votes
0 answers

Adapter pattern in C++ using mix of private and public inheritance

I have a question about creating a lock adapter, and more generally the adapter pattern with style of implementation below. The implementation below based on the answer in Proper C++ way of implementing a lock_guard for custom library. The…
user3882729
  • 1,339
  • 8
  • 11
0
votes
1 answer

Why std::lock() causes the endless loop when works with my own unique_lock object?

I am trying to implement the analog of unique_lock (it's just studying task, I understand that standard library implementation works perfectly). I've already written all the methods that I need and now I am trying to test my code on the example from…
0
votes
0 answers

Segmentation fault when executing member method from thread

I am trying to learn the std::threads, std::mutex and std::unique_lock. The idea is to create 5 threads. Requests will be written to a queue and the 5 threads will pick up request from the queue and process it. If the queue is empty the threads will…
david
  • 485
  • 1
  • 4
  • 10
0
votes
0 answers

Having multiple reader locks in a single thread

I have data coupled with a Lock = boost::shared_mutex. I am locking data access with reader locks ReadLock = boost::shared_lock and writer locks WriteLock = boost::unique_lock. Obviously, lots of readers may be reading the data at a…
Bill Kotsias
  • 3,258
  • 6
  • 33
  • 60
0
votes
2 answers

Is it safe to wait/notify on unique_lock mutexes when same mutexes are used with lock_guard on somewhere else?

I was using following kind of wait/signal way to let threads inform each other. std::condition_variable condBiz; std::mutex mutexBar; .. void Foo::wait() { std::unique_lock waitPoint(mutexBar); if (waitPoint.owns_lock()) { …
huseyin tugrul buyukisik
  • 11,469
  • 4
  • 45
  • 97
0
votes
1 answer

unique_lock compilation error brace initializer

I'm perplexed... I have a piece of code thus; class logger { public: std::mutex mut; unique_lock< std::mutex> lk(mut); // ... snip ... } The line "unique_lock < std::mutex > lk (mut) " fails compilation with this error; **g++ -pthread…
user3613174
  • 669
  • 1
  • 7
  • 13
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

Thread notification isn't working for my consumer

I have an attempt at a producer/consumer Producer #pragma once #ifndef PRODUCER_H #define PRODUCER_H #include #include "Mailbox.h" class Producer { private: std::thread producer; Mailbox& mailbox; public: Producer(Mailbox&…
Mushy
  • 2,535
  • 10
  • 33
  • 54