简体   繁体   中英

Boost thread fails BOOST_ASSERT( px != 0 );

I have created a future object, as such:

Future.h

#ifndef FUTURE_H_
#define FUTURE_H_

#include "../interfaces/IFuture.h"
#include <stdio.h>
#include <boost/thread.hpp>

using namespace boost;

class Future: public IFuture {
private:
    void** data;
    bool isDataReady;
    mutex mut;
    condition_variable cond;

public:
    Future();

    ~Future();

    bool isReady();

    void setData(void* data[]);

    void** getData();
};

#endif /* FUTURE_H_ */

Future.cpp

#include "../headers/Future.h"

Future::Future(){
    this->data = NULL;
    this->isDataReady = false;
}

Future::~Future(){
    delete [] data;
}

bool Future::isReady(){
    return isDataReady;
}

void Future::setData(void* data[]){
    if(isDataReady)
        return;
    {
        lock_guard<mutex> lock(mut);
        this->data = data;
        isDataReady = true;
    }
    cond.notify_one();
}

void** Future::getData(){
    unique_lock<mutex> lock(mut);
    while(!isDataReady){
        cond.wait(lock);
    }
    return data;
}

The main application creates multiple Future objects as needed. The first object work fine, but around the one-hundredth Future object the condition.wait(mut) fails the BOOST_ASSERT( px != 0 ); in intrusive_ptr.hpp.

I do not understand why this is happening.

I am using boost thread in windows on the mingw g++ compiler.

In Future::setData you're unlocking a mutex you don't hold (becuase it's taken care of by the lock_guard object):

void Future::setData(void* data[]){
    if(isDataReady)
        return;
    {
        lock_guard<mutex> lock(mut);
        this->data = data;
        isDataReady = true;
    }
    mut.unlock();       // <=== remove this
    cond.notify_one();
}

Problem "fixed". Started using Visual Studio and its compiler. Boost thread and MinGW are not very compatible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM