简体   繁体   中英

Race Condition in detached Thread

I have tried to find a similar problem, but was unable to find it, or my knowledge is not enough to recognize the similarity.

I have a main loop creating an object, whereas this object has an infinite loop to process a matrix and do stuff with this matrix. I call this process function within a separate thread an detach it, so it is able to process the matrix multiple times, while the main loop might just wait for something and does nothing.

After a while the main loop receives a new matrix, while I represented it by just creating a new matrix and passes this new matrix into the object. The idea is that, due to waiting a few seconds before processing in the infinite while loop again, the update function can lock the mutex and the mutex is not (almost) frequently locked.

Below I tried to code a minimal Example.

class Test
    {
    public:
        Test();
        ~Test();

    void process(){
        while(1){
             boost::mutes::scoped_lock locker(mtx);
             std::cout << "A" << std::endl;
             // do stuff with Matrix
             std::cout << "B" << std::endl;
             mtx.unlock();
             //wait for few microseconds
        }
    }

    void updateMatrix(matrix MatrixNew){
        boost::mutes::scoped_lock locker(mtx);
        std::cout << "1" << std::endl;
        Matrix = MatrixNew;
        std::cout << "2" << std::endl;
    }

private:
    boost::mutex mtx;
    matrix Matrix;
}

int main(){
Test test;
boost::thread thread_;

thread_ = boost::thread(&Test::process,boost::ref(test));
thread_.detach();

while(once_in_a_while){
    Matrix MatrixNew;
    test.updateMatrix(MatrixNew);
}
}

Unfortunately a race condition occurs. Process and Update have multiple steps within the locked mutex environment, while I print stuff to the console between these steps. I found that, both, the matrix is messed up and Letters/Numbers to occur parallel and not consecutive.

Any ideas why this occurs?

Best wishes and thanks in advance

    while(1){
         boost::mutes::scoped_lock locker(mtx);
         std::cout << "A" << std::endl;
         // do stuff with Matrix
         std::cout << "B" << std::endl;
         mtx.unlock();
         //wait for few microseconds
    }

Here you manually unlock mtx . Then sometime later the scoped_lock (called locker ) also unlocks the mutex in its destructor (which is the point of that class). I dont know what the guarantee's boost::mutex requires but unlocking it more times than you locked it can't lead to anything good.

Instead of mtx.unlock(); you presumably want locker.unlock();

Edit: A recommendation here would be to avoid using boost for this and use standard c++ instead. thread ing has been part of the standard since C++11 (8 years!) so presumably all your tools will now support it. Using standardised code/tools gives you better documentation and better help as they're much more well known. I'm not knocking boost (a lot of the standard started in boost) but once something has been consumed into the standard you should strongly think about using it.

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