std::mutex locker;
locker.lock();
// Here is the threading at the end of which will be called locker.unlock();
locker.lock();
std::mutex locker;
boost::thread t([&]() {
locker.lock();
});
t.join();
...
locker.lock();
An example I wrote very conventional,
locker.lock();
locker.lock();
According to the logic because in this case, the first lock the mutex should be locked and the second lock, the program should wait until it will be unlocked again to lock it
I'm not confused?
In fact, if two consecutive call lock, then the program should hang, but should not fall out an exception? Or is it some kind of protection system from deadlock? If so, how to circumvent it?
If lock is called by a thread that already owns the mutex, the behavior is undefined: for example, the program may deadlock. An implementation that can detect the invalid usage is encouraged to throw a std::system_error with error condition resource_deadlock_would_occur instead of deadlocking.commented on June 10th 19 at 14:47
Find more questions by tags C++
Just crutch :)
I use a class, which causes several streams, one within another
And I need to wait until all threads will do their work and then move on with the main program
There is a class https request
Code is something like this:
And in result info for a as a becomes unavailable, because the main thread terminates and destroys all the local variables
Of course you can pass your variables in the function as well, but it is too gemorno since I use a lot of local variables (I for example just one variable written)
So it is easier just to keep the main thread until the callback completes - rigoberto commented on June 10th 19 at 14:42
For this is join streams, shove them into an array and wait until the completion of all. In General, for network requests better to use async and not threads. - Zit commented on June 10th 19 at 14:45
I know about join, and about the future&async
Just the task is very specific and requires that the implementing class
Here is the rate for sending rate request
And there is quite a complex class is doing
Just in this piece of code I don't really use the appointment class
The point is that these 2-3 places, where do not have to use the class
But the meaning for these 2-3 places to write a separate class when these pieces of code are invoked very rarely, and there is not required neither the speed nor optimized? Pointless, because you can hostility :)
+ I was interested to find out what's wrong with the second lock, which determined the creation of a question on the toaster
PS just in case I will specify that the program is written for personal use, so that you can a bit of dirt in the code to prevent - rigoberto commented on June 10th 19 at 14:51
The main argument is that the callback is sent in a separate thread, while the class will complete the necessary actions
In addition, for single request->query may create several nested within other flows. And in this callback may not be called directly from the current thread, since the class can send the request in a queue and then they will be doing another stream which runs done independently
In short, not an option
And at the expense of locking... Why am I unable to apply them in this case? Somewhere it is written "is used for synchronizing data access", doesn't mean that you can't use locks to synchronize threads? Or there are some serious arguments against?
And by the way, I'm not sure, but it seems I still trying just "to synchronize access to data." Give the thread access to the data before it will get the memory optimizer :) - rigoberto commented on June 10th 19 at 14:54
But is this crutch works? You start the thread that locks the mutex. Immediately after waiting a thread to terminate and then again try to lock the mutex, but in the main thread. And at this point the program hangs, because the mutex is already locked and the main thread will wait for unlock it. How do you release the mutex to other threads?
Not trying in some way to blame or to convince myself wondering how this problem can be solved in other ways. - bernhard36 commented on June 10th 19 at 14:57
It is implied that there will be made unlock
The complete code looks something like this:
The main thread will exist until callback will finish its work. Certainly good need to pass variables to the callback, and not to hold local variables. But in this case ovchinka not worth the candle :) - rigoberto commented on June 10th 19 at 15:03
Just as I said, in this case this is not asynchronous
There is not time saving
The class has its own methods, how to run faster
As I said, I'm not really on purpose I use this class in this place, so we have to use these crutches
When I use the class in the right place, it makes no sense to keep the main stream, as it sends the request, and then he made him not care. The task of the main thread just call a method - rigoberto commented on June 10th 19 at 15:06
In General, without knowledge of the specific tasks is difficult to advise anything. - Zit commented on June 10th 19 at 15:09