I have this code which is giving the error:
error this boost::lock_error
error this boost::lock_error
error this boost::lock_error
error this boost::lock_error
error this boost::lock_error
Since I don't know where is the error I'll try to copy the code:
Main
boost::tuple<ppa::Node*, ppa::Node*, ppa::Node*, bool> tuple = dups.back();
ppa::Node *n = boost::get<0>(tuple);
cout << "creating other threads" << endl;
for ( int i = 0; i < 5; ++i )
{
cout << "making thread " << i << endl;
g.create_thread( boost::bind( threaded_function, boost::ref(mf),
boost::ref(n) ));
}
Threads
void threaded_function(Model_factory &mf, ppa::Node *root)
{
try
{
while(true)
{
boost::mutex::scoped_lock lock(result_mutex);
if(wait.empty())
{
lock.unlock();
break;
}
else
{
lock.lock();
if(!running_jobs.empty())
{
cout << "vector wait size = " << wait.size() << "\n";
cout << "running size = " << running_jobs.size() << "\n";
cout << "done size = " << done.size() << "\n";
boost::tuple<ppa::Node*, ppa::Node*, ppa::Node*, bool> tuple;
tuple = running_jobs.back();
running_jobs.pop_back();
...
}
...
}
...
}
}
catch (boost::lock_error& le)
{
cout << "error this " << le.what() << endl;
}
}
The code is used in a alignment program I'm trying to do parallel, it aligns the tuples one by one in different threads, until the waiting list (vector wait) is empty. Thanks.
I have done something I put a lock.unlock() before every if and else and its working now, but why??
You are locking the mutex twice.
boost::mutex::scoped_lock lock(result_mutex);
locks the mutex for you. It will also unlock it when it goes out of scope, so you don't need to unlock it by yourself.
So the following lines are unnecessary:
lock.lock();
lock.unlock();
Related
The following code reasults in a deadlock. The problem is that I cannot figure out how unlock the consumers waiting on the condition variable. The consumer should loop and consume from the stack when a certain condition is met. I've tried exiting when the stack is empty but of course it doesn't work.
Stack.h
class Stack {
private:
std::stack<int> stack;
std::mutex mutex;
std::condition_variable is_empty;
bool done;
public:
Stack();
void push(int);
void pop();
void print();
bool isDone() const;
~Stack();
};
Stack.cpp
#include <iostream>
#include <sstream>
#include <thread>
#include "Stack.h"
void Stack::push(int x) {
std::lock_guard lock(mutex);
std::stringstream msg1;
msg1 << "producer " << std::this_thread::get_id() << " pushing " << x << std::endl;
std::cout << msg1.str();
stack.push(x);
std::stringstream msg;
msg << "producer " << std::this_thread::get_id() << ": " << x << " pushed" << std::endl;
std::cout << msg.str();
is_empty.notify_all();
}
void Stack::pop() {
std::unique_lock lock(mutex);
std::stringstream msg;
msg << "consumer " << std::this_thread::get_id() << " waiting to consume" << std::endl;
std::cout << msg.str();
is_empty.wait(lock, [this] { return !stack.empty(); });
if (!stack.empty()) {
stack.pop();
std::stringstream msg1;
msg1 << "consumer " << std::this_thread::get_id() << " popped" << std::endl;
std::cout << msg1.str();
} else {
done = true;
is_empty.notify_all();
}
}
void Stack::print() {
std::lock_guard lock(mutex);
for (int i = 0; i < stack.size(); i++) {
std::cout << "\t" << stack.top() << std::endl;
}
}
Stack::~Stack() {
}
bool Stack::isDone() const {
return done;
}
Stack::Stack() : done(false) {}
main.cpp
#include <thread>
#include <vector>
#include <iostream>
#include "Stack.h"
int main() {
Stack stack;
std::vector<std::thread> producer;
std::vector<std::thread> consumer;
for (int i = 0; i < 10; i++) {
consumer.emplace_back([&stack]{
while (!stack.isDone()) {
stack.pop();
}
});
}
for (int i = 0; i < 1; i++) {
producer.emplace_back([&stack]{
for (int j = 0; j < 5; ++j) {
stack.push(random());
}
});
}
for (int k = 0; k < producer.size(); k++) {
producer[k].join();
std::cout << producer[k].get_id() << " joined" << std::endl;
stack.print();
}
for (int j = 0; j < consumer.size(); j++) {
consumer[j].join();
std::cout << consumer[j].get_id() << " joined" << std::endl;
stack.print();
}
return 0;
}
Your code is not deadlocked but your threads are waiting for more input because you haven't configure the value of done properly.
There is no way that the else condition is invoked here
is_empty.wait(lock, [this] { return !stack.empty(); });
if (!stack.empty()) {
stack.pop();
std::stringstream msg1;
msg1 << "consumer " << std::this_thread::get_id() << " popped" << std::endl;
std::cout << msg1.str();
} else {
done = true;
is_empty.notify_all();
}
Looking from the code it seems like what you want is that after the producer stops producing the consumer should wake up and empty. But this is not the way to implement it. After the producer has pushed 5 elements you should set done =true from there.
Also as answered by madducci you need to change the location of notify_all();
This is something which worked for me
is_empty.wait(lock, [&] { return stack.size()>0 || done; });
if (!stack.empty()) {
int val=stack.top();
stack.pop();
std::stringstream msg1;
msg1 << "consumer " << std::this_thread::get_id() << " popped " <<val<<std::endl;
std::cout << msg1.str();
}
Looks like you have a logic error in your pop function: you never call notify_all() in case you pop an element from the stack.
The correct way should be this one:
void Stack::pop() {
std::unique_lock lock(mutex);
std::stringstream msg;
msg << "consumer " << std::this_thread::get_id() << " waiting to consume" << std::endl;
std::cout << msg.str();
is_empty.wait(lock, [this] { return !stack.empty(); });
if (!stack.empty()) {
stack.pop();
std::stringstream msg1;
msg1 << "consumer " << std::this_thread::get_id() << " popped" << std::endl;
std::cout << msg1.str();
} else {
done = true;
}
is_empty.notify_all();
}
You also invoke pop() before push() in your main
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 https://en.cppreference.com/w/cpp/thread/unique_lock/unique_lock.
When it comes to std::lock(lk_b, lk_c); the infinite loop starts.
I did some cout's to understand where the program loses control and the result is the following: lock -> try -> unlock -> lock -> try -> unlock.
Here is partial unique_lock implementation (I included only those methods, which are used in the problem part of the example).
template<typename Mutex>
class my_unique_lock {
Mutex *lockable;
bool is_acquired;
public:
explicit my_unique_lock(Mutex& m): lockable{&m}, is_acquired{true}{
lockable->lock();
//std::cout << "constructor my_unique_lock(Mutex& m)" << std::endl;
}
my_unique_lock(Mutex& m, std::defer_lock_t t): lockable{&m}, is_acquired{false}{
std::cout << "constructor my_unique_lock(Mutex& m, std::defer_lock_t t)" << std::endl;
}
bool try_lock(){
std::cout << "try_lock" << std::endl;
if(lockable == nullptr)
throw std::system_error();
is_acquired = mutex()->try_lock();
return is_acquired;
}
void lock(){
std::cout << "lock" << std::endl;
if(lockable == nullptr || owns_lock())
throw std::system_error();
mutex()->lock();
is_acquired = true;
}
void unlock(){
//std::cout << "unlock" << std::endl;
if(lockable == nullptr || !owns_lock())
throw std::system_error();
mutex()->unlock();
is_acquired = false;
std::cout << "unlocked" << std::endl;
}
Mutex *mutex() const noexcept {
//std::cout << "*mutex()" << std::endl;
return lockable;
}
bool owns_lock() const noexcept {
//std::cout << "owns_lock()" << std::endl;
return lockable != nullptr && is_acquired;
}
~my_unique_lock(){
//std::cout << "destructor" << std::endl;
if(mutex() != nullptr && owns_lock()){
mutex()->unlock();
is_acquired = false;
}
}
};
And here is the example.
void update(std::mutex &m_a, std::mutex &m_b, std::mutex &m_c, int &a, int &b, int &c)
{
{
my_unique_lock<std::mutex> lk(m_a);
a++;
}
{
my_unique_lock<std::mutex> lk_b(m_b, std::defer_lock);
my_unique_lock<std::mutex> lk_c(m_c, std::defer_lock);
std::lock(lk_b, lk_c);
b = std::exchange(c, b + c);
}
}
int main()
{
std::mutex m_a, m_b, m_c;
int a, b, c = 1;
std::vector<std::thread> threads;
for (unsigned i = 0; i < 1; ++i)
threads.emplace_back(update, std::ref(m_a), std::ref(m_b), std::ref(m_b), std::ref(a), std::ref(b), std::ref(c));
for (auto& i: threads)
i.join();
std::cout << a << "'th and " << a+1 << "'th Fibonacci numbers: "
<< b << " and " << c << '\n';
}
So, as I said, I don't really understand why lock() causes the endless loop with such a chain of calls (lock -> try_lock -> unlocked).
Change std::ref(m_b), std::ref(m_b) to std::ref(m_b), std::ref(m_c). Copy/paste typo.
Your std::lock is trying to lock m_b twice.
Other issues: you violate rule of 0/3/5. You have multiple different near-identical lock fiddling for lock/unlock code (refactor).
I'm using multiple boost::asio::deadline_timer on one io_service object. std::shared_ptr of boost::asio::deadline_timer are stored in the container std::map<int, std::shared_ptr<debug_tim>> timers with index.
In the timer handler, I erase other boost::asio::deadline_timer. However, it seems that the erased timer woule be often fired with success error code.
Is there any way to avoid that. I expect that the timer handler that corresponding to the erased boost::asio::deadline_timer always fires with Operation canceled.
Am I missing something?
Here is the code that reproduces the behavior
https://wandbox.org/permlink/G0qzYcqauxdqw4i7
#include <iostream>
#include <memory>
#include <boost/asio.hpp>
// deadline_timer with index ctor/dtor print
struct debug_tim : boost::asio::deadline_timer {
debug_tim(boost::asio::io_service& ios, int i) : boost::asio::deadline_timer(ios), i(i) {
std::cout << "debug_tim() " << i << std::endl;
}
~debug_tim() {
std::cout << "~debug_tim() " << i << std::endl;
}
int i;
};
int main() {
boost::asio::io_service ios;
std::map<int, std::shared_ptr<debug_tim>> timers;
{
for (int i = 0; i != 5; ++i) {
auto tim = std::make_shared<debug_tim>(ios, i);
std::cout << "set timer " << i << std::endl;
tim->expires_from_now(boost::posix_time::seconds(1));
timers.emplace(i, tim);
tim->async_wait([&timers, i](auto ec){
std::cout << "timer fired " << i << " : " << ec.message() << std::endl;
auto it = timers.find(i);
if (it == timers.end()) {
std::cout << " already destructed." << std::endl;
}
else {
int other_idx = i + 1; // erase other timer (e.g. i + 1)
timers.erase(other_idx);
std::cout << " erased " << other_idx << std::endl;
}
}
);
}
}
ios.run();
}
I also call boost::asio::deadline_timer::cancel() before I erase the timer. However, I got similar result. Here is the cancel version:
https://wandbox.org/permlink/uM0yMFufkyn9ipdG
#include <iostream>
#include <memory>
#include <boost/asio.hpp>
// deadline_timer with index ctor/dtor print
struct debug_tim : boost::asio::deadline_timer {
debug_tim(boost::asio::io_service& ios, int i) : boost::asio::deadline_timer(ios), i(i) {
std::cout << "debug_tim() " << i << std::endl;
}
~debug_tim() {
std::cout << "~debug_tim() " << i << std::endl;
}
int i;
};
int main() {
boost::asio::io_service ios;
std::map<int, std::shared_ptr<debug_tim>> timers;
{
for (int i = 0; i != 5; ++i) {
auto tim = std::make_shared<debug_tim>(ios, i);
std::cout << "set timer " << i << std::endl;
tim->expires_from_now(boost::posix_time::seconds(1));
timers.emplace(i, tim);
tim->async_wait([&timers, i](auto ec){
std::cout << "timer fired " << i << " : " << ec.message() << std::endl;
auto it = timers.find(i);
if (it == timers.end()) {
std::cout << " already destructed." << std::endl;
}
else {
int other_idx = i + 1; // erase other timer (e.g. i + 1)
auto other_it = timers.find(other_idx);
if (other_it != timers.end()) {
other_it->second->cancel();
timers.erase(other_it);
}
std::cout << " erased " << other_idx << std::endl;
}
}
);
}
}
ios.run();
}
Edit
Felix, thank you for the answer. I understand the boost::asio::deadline::timer::cancel() behavior. I always need to care the lifetime of boost::asio::deadline::timer. I my actual code of my project, the ``boost::asio::deadline::timer` is a member variable of another object such as a session object. And in the timer handler, it accesses the object. It's dangerous.
I consider how to write safe code. And I come up with using std::weak_ptr in order to check the object's lifetime.
Here is the updated code:
#include <iostream>
#include <memory>
#include <boost/asio.hpp>
// deadline_timer with index ctor/dtor print
struct debug_tim : boost::asio::deadline_timer {
debug_tim(boost::asio::io_service& ios, int i) : boost::asio::deadline_timer(ios), i(i) {
std::cout << "debug_tim() " << i << std::endl;
}
~debug_tim() {
std::cout << "~debug_tim() " << i << std::endl;
}
int i;
};
int main() {
boost::asio::io_service ios;
std::map<int, std::shared_ptr<debug_tim>> timers;
{
for (int i = 0; i != 5; ++i) {
auto tim = std::make_shared<debug_tim>(ios, i);
std::cout << "set timer " << i << std::endl;
tim->expires_from_now(boost::posix_time::seconds(1));
timers.emplace(i, tim);
// Capture tim as the weak_ptr wp
tim->async_wait([&timers, i, wp = std::weak_ptr<debug_tim>(tim)](auto ec){
std::cout << "timer fired " << i << " : " << ec.message() << std::endl;
// Check the lifetime of wp
if (!wp.lock()) std::cout << " timer freed." << std::endl; // return here on actual code
auto it = timers.find(i);
if (it == timers.end()) {
std::cout << " already destructed." << std::endl;
}
else {
int other_idx = i + 1; // erase other timer (e.g. i + 1)
timers.erase(other_idx);
std::cout << " erased " << other_idx << std::endl;
}
}
);
}
}
ios.run();
}
Is this a good way to avoid accessing the deleted object that has the boost::asio::deadline_timer ?
Edit
My weak_ptr solution works well.
See
How to avoid firing already destroyed boost::asio::deadline_timer
According to the reference of deadline_timer::cancel:
If the timer has already expired when cancel() is called, then the handlers for asynchronous wait operations will:
have already been invoked; or
have been queued for invocation in the near future.
These handlers can no longer be cancelled, and therefore are passed an error code that indicates the successful completion of the wait operation.
We can know that calling cancel() can not cancel the timer which has already been queued for firing.
And it seems that the dealine_timer doesn't override destructor. (There is no destructor in the member list of deadline_timer)
In your code snippet, all timers will fire at almost the same time. Concerning that asio will use some internal threads, it's quite probably that when one completion handler is called, the others are being queued.
I've added some slight multi threading to a simple c++ program and have encountered a few issues along the way.
The latest of these issues is that historical::assignthreads for some reason is receiving an empty vector from the function historical::writeData.
Looking at the code below you will see that writeData iterates through a vector and puts the data in a placeholder before sending it forward to assignthreads (after 5 iterations) - meaning that the vector being sent from writeData to assignthreads shouldn't be empty.
However in assignthreads you will see that there are two cout:s, one before and one after the loop. Both writes to cout without the loop even starting.
Does anyone have any idea of how this could be the case?
void historical::writeData(std::vector<std::vector<std::wstring>> in, const string& symbol) {
std::cout << "Sending data to database connector" << std::endl;
std::vector<std::vector<std::wstring>> temp;
std::vector<std::vector<std::wstring>>::iterator it;
int count = 0;
for (it = in.begin(); it != in.end(); it++) {
if (count = 5) {
cout << "I'm in count 5" << endl;
assignthreads(temp, symbol);
temp.clear();
count = 0;
}
else {
cout << "I'm in count 0" << endl;
temp.push_back(*it);
count++;
}
}
if (!temp.empty()) {
cout << "I'm in empty" << endl;
assignthreads(temp, symbol);
}
else cout << "I'm empty!!" << endl;
}
void historical::assignthreads(std::vector<std::vector<std::wstring>>& partVec, const string& symbol) {
int i = 0;
cout << "I'm in assign" << endl;
vector<thread> threads(size(partVec));
std::vector<std::vector<std::wstring>>::iterator it;
for (it = partVec.begin();
it != partVec.end();
it++) {
cout << "I'm in the loop" << endl;
std::shared_ptr<database_con> sh_ptr(new database_con);
threads.at(i) = std::thread(&database_con::start, sh_ptr, *it, symbol);
i++;
}
cout << "I've finished" << endl;
for (auto& th : threads) th.join();
}
void historical::writer(string* pInput) {
ofstream mf("test.csv");
if (mf.is_open()) {
mf << *pInput;
mf.close();
}
else cout << "Unable to open file" << endl;
}
Your fundamental problem here is that count = 5 is an assignment and is therefore always true. You intended to use count == 5.
It's worth noting that particularly as your vector becomes large copying it is very wasteful, and you're doing this 2 ways:
The vector is passed into writeData by value, change to copying by reference: void writeData(std::vector<std::vector<std::wstring>>& in, const string& symbol)
temp will eventually copy every element of in, use iterators instead so your code would have to change to:
#define SIZE 5
void assignthreads(std::vector<std::vector<std::wstring>>::iterator start, std::vector<std::vector<std::wstring>>::iterator finish, const string& symbol) {
cout << "I'm in assign" << endl;
vector<thread> threads(distance(start, finish));
for(auto i = 0; start != finish; ++i, ++start) {
cout << "I'm in the loop" << endl;
std::shared_ptr<database_con> sh_ptr(new database_con);
threads.at(i) = std::thread(&database_con::start, sh_ptr, *start, symbol);
}
cout << "I've finished" << endl;
for (auto& th : threads) th.join();
}
void writeData(std::vector<std::vector<std::wstring>>& in, const string& symbol) {
std::cout << "Sending data to database connector" << std::endl;
auto count = 0;
while(count < in.size() - SIZE) {
auto start = next(in.begin(), count);
count += SIZE;
auto finish = next(in.begin(), count);
assignthreads(start, finish, symbol);
}
assignthreads(next(in.begin(), count), in.end(), symbol);
cout << "I'm empty!!" << endl;
}
I'm doing c++ and the code is giving me these errors:
new size of the done 64
getting next item to do (0x90b9c0 0x90bab0 0x90be40 0)
vector wait size = 0
running size = 1
done size = 64
done this align #27# #26# Gorilla_gorilla_2
setting the done tuple to 1 to get them next
new size of the done 65
getting next item to do (0xbff3897200000000 0xbfda1b75f84c0030 0x30 17)
vector wait size = 18446744073709551615
running size = 1
done size = 65
getting next item to do (0xbfdcc288867fc50b 0x31 0x1 1)
/usr/local/netbeans-7.2rc1/ide/bin/nativeexecution/dorun.sh: line 33: 8234
Segmentation
fault (core dumped) sh "${SHFILE}"
And I wonder how can I get more details of the seg fault, like the line that caused it, to fix it, thanks.
Edit:
bool tuple_compare(boost::tuple< ppa::Node*, ppa::Node*, ppa::Node*, bool> tuple)
{
if( boost::get< 3 >( tuple) == true)
{
return true;
}
else
{
return false;
}
}
void threaded_function(Model_factory &mf, ppa::Node *root)
{
try {
while(true)
{
boost::mutex::scoped_lock lock(result_mutex);
if(wait.empty())
{
lock.unlock();
break;
}
else {
lock.unlock();
lock.lock();
if(!running_jobs.empty())
{
cout << "vector wait size = " << wait.size() << endl;
cout << "running size = " << running_jobs.size() << endl;
cout << "done size = " << done.size() << endl;
boost::tuple<ppa::Node*, ppa::Node*, ppa::Node*, bool> tuple = running_jobs.back();
running_jobs.pop_back();
lock.unlock();
ppa::Node *father = boost::get<0>(tuple);
ppa::Node *first_son = boost::get<1>(tuple);
ppa::Node *second_son = boost::get<2>(tuple);
bool flag = boost::get<3>(tuple);
father->start_alignment_new(&mf);
lock.lock();
cout << "done this align " << father->get_name() << " " << first_son->get_name() << " " << second_son->get_name()
<< endl;
cout << "setting the done tuple to 1 to get them next" << endl;
done.push_back(tuple);
cout << "new size of the done " << done.size() << endl;
lock.unlock();
}
else {
lock.unlock();
lock.lock();
cout << "getting next item to do " << wait.back() << endl;
boost::tuple<ppa::Node*, ppa::Node*, ppa::Node*, bool> run = wait.back();
running_jobs.push_back(run);
wait.pop_back();
lock.unlock();
}
}
}
}
catch (boost::lock_error& le)
{
cout << "error this " << le.what() << endl;
}
}
Main:
int main(int argc, char *argv[])
{
clock_t t_start=clock();
ppa::Node* parent;
ppa::Node* left;
ppa::Node* rigth;
ppa::Node* check_root;
/*
...
boost::tuple<ppa::Node*, ppa::Node*, ppa::Node*, bool> tuple = dups.back();
ppa::Node *n = boost::get<0>(tuple);
//cout << "creating waiting thread" << endl;
// g.create_thread( boost::bind(wait_function));
cout << "creating other threads" << endl;
for ( int i = 0; i < 5; ++i )
{
cout << "making thread " << i << endl;
g.create_thread( boost::bind( threaded_function, boost::ref(mf), boost::ref(n) ));
}
cout << g.size() << endl ;
// wait for them
// sleep(15);
g.join_all();
I'm pretty sure it comes form this part that is the new I have added.
Bad code, too many locks and unlocks, mutex has a lock method no need to use shared lock.lock()