Boost logo

Boost Users :

Subject: Re: [Boost-users] Fw: Use of QMutex not as expected
From: Gabriel Redner (gredner_at_[hidden])
Date: 2015-08-25 15:15:34


The problem appears to be that each of your threads is locking a
different mutex, and so there is no synchronization among them.
Probably you want your worker classes to store myResource by reference
rather than by value (and eliminate the copy-constructor of
myResource, and better yet make it non-copyable). That way there will
be only one myResource object, and only one mutex.

-Gabe

On Tue, Aug 25, 2015 at 7:31 AM, Don El Ron <donelron_at_[hidden]> wrote:
> Dear all,
>
> I use the following code:
>
> __________________________________________________________________
> #include <boost/thread/mutex.hpp>
> #include <boost/thread.hpp>
> class myRessource{
> public:
> myRessource() : m_iValue(0){}
>
> //copy ctor necessary, so a member of type boost::mutex is possible in
> this class
> myRessource(const myRessource &copyRes) : m_iValue(0){}
> ~myRessource(){}
> void SendCommand(int ival){
> myMutex.lock();
> m_iValue=ival;
> std::cout << "resource update to " << ival << " active count is " <<
> myMutex.active_count << std::endl;
> myMutex.unlock();
> }
> private:
> int m_iValue;
> boost::mutex myMutex;
> };
>
> int iSleepTime1=200;
> int iSleepTime2=30;
>
> class myWorker1
> {
> public:
> myWorker1(myRessource res): m_res(res){}
> ~myWorker1(){}
> void DoWork(void){
> m_res.SendCommand(1);
> boost::this_thread::sleep(boost::posix_time::milliseconds(iSleepTime1));
> m_res.SendCommand(2);
> boost::this_thread::sleep(boost::posix_time::milliseconds(iSleepTime1));
> m_res.SendCommand(3);
> boost::this_thread::sleep(boost::posix_time::milliseconds(iSleepTime1));
> m_res.SendCommand(4);
> boost::this_thread::sleep(boost::posix_time::milliseconds(iSleepTime1));
> }
> private:
> myRessource m_res;
> };
>
> class myWorker2
> {
> public:
> myWorker2(myRessource res): m_res(res){}
> ~myWorker2(){}
> void DoWork(void){
> m_res.SendCommand(201);
> boost::this_thread::sleep(boost::posix_time::milliseconds(iSleepTime2));
> m_res.SendCommand(202);
> boost::this_thread::sleep(boost::posix_time::milliseconds(iSleepTime2));
> m_res.SendCommand(203);
> boost::this_thread::sleep(boost::posix_time::milliseconds(iSleepTime2));
> m_res.SendCommand(204);
> boost::this_thread::sleep(boost::posix_time::milliseconds(iSleepTime2));
> }
> private:
> myRessource m_res;
> };
>
> int main(int argc, char *argv[])
> {
> myRessource r;
> myWorker1 w1(r);
> boost::thread workerThread1(&myWorker1::DoWork, &w1);
>
> myWorker2 w2(r);
> boost::thread workerThread2(&myWorker2::DoWork, &w2);
> workerThread1.join();
> workerThread2.join();
> }
> ___
> _______________________________________________________________
> This results in the following output (no formatting, e.g. line breaks done):
>
> resource update to resource update to 201 active count is -21474836481
> active co
> unt is -2147483648
> resource update to 202 active count is -2147483648
> resource update to 203 active count is -2147483648
> resource update to 204 active count is -2147483648
> resource update to 2 active count is -2147483648
> resource update to 3 active count is -2147483648
> resource update to 4 active count is -2147483648
>
>
> The output always looks like this, i. e. the first two lines are "merged",
> whereas the remaining lines look just fine.
> Why does this happen and what do I have to do in order to correct my
> mistake???
> Many thanks in advance!
>
>
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net