Myth is blocked despite unlock



  •     class ThreadQ
        {
        public:
            ThreadQ() : t_(&ThreadQ::qwe, this){}
    
            ~ThreadQ(){ run_ = false; t_.join(); }
    
    
        private:
            void qwe()
            {
                cout << "qwe" << endl;
    
                boost::unique_lock< boost::mutex > lock(mutex_);
    
                cout << "run=" << run_ << endl;
    
                while (run_)
                {
                    cout << 33;
    
                    lock.unlock();
                    lock.lock();
                }
            }
    
            thread t_;
            boost::mutex mutex_;
            bool run_ = true;
    
        };
    
    
    int main()
    {
        ThreadQ t;
    
        system("pause");
    }
    

    In 80 per cent of the cases, this code in the visual studio 15 led to a deadlock, 10 per cent of the flow was not started, and 10 per cent was normal... (the project was fully retrieved several times)

    However, after rebooting, the problems were left for half an hour, what could they be caused? Windows 7

    Version boost: 1.60.0

    Compilile Bust:

    b2 --stagedir=C:\boost_1_60_0cs_w address-model=32 -j4 --build-type=complete runtime-link=static define=BOOST_USE_WINAPI_VERSION=0x0501
    

    Compiler version: 14.00.23506.0



  • The initialization of the class fields is determined by the procedure for their declaration. In your case, it comes first. thread Then mutex♪ There's a flow on your design initialization list, which is a function. qwewhich uses myutex. And from time to time, it appears that by the time of use in the second flow, the muytex had not yet been initialized. What makes phantom glues.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2