[Boost-users] [thread] : pause/resume thread

119 views
Skip to first unread message

Kazz

unread,
Apr 15, 2011, 6:06:56 AM4/15/11
to boost...@lists.boost.org
Hi All, I have just started learning boost. I want my thread to support following functions
Pause();
Resume();
Stop();.
I have come up with following code, which ends up as dead lock. Can some one please guide me as how to achieve the desired functionality.
Would really appreciate any help.
Thanks.
Kaz
class qthread
{
public:
    boost::thread* mThread;
    boost::condition_variable mCond;
    boost::mutex mMutex;
    bool mExit;
    qthread(): mExit(false)
    {
        // create function pointer to local Execute function
        boost::function<void (qthread*)> fptr = &qthread::Execute;
        fptr(this);
        // pass on function pointer along with this reference to boost thread
        boost::thread th0( fptr, this);       
        // assign temp thread to local variable
        mThread = &th0;
    }
    void Resume()
    {
        mMutex.unlock();
        mCond.notify_all();
    }
    void Stop()
    {
        mThread->interrupt();
        mExit = true;
        Resume();
    }
     void Pause()
    {
     // todo
    }
    virtual void Execute()
    {
        std::cout<<"Entering thread Execution"<<std::endl;
        boost::unique_lock<boost::mutex> lock(mMutex);               
        mCond.notify_all();       
        mCond.wait(lock);       //<< DEAD LOCK   
        int i = 0;
        while ( !mExit )
        {                   
            try
            {
                while ( 1 )
                {
                    std::cout<<"Executing thread, cycle i = "<<++i<<std::endl;
                    boost::this_thread::interruption_point();
                }
            }
            catch( boost::thread_interrupted&)
            {
                std::cout<<"thread_interrupt exception"<<std::endl;
                mCond.wait(lock);
            }
        }
        std::cout<<"Exiting thread"<<std::endl;
    }
};
//--------------------------
int main()
{
    qthread qt;   ///<<  DEAD LOCK
    boost::this_thread::sleep(boost::posix_time::seconds(3) );
    qt.Resume();
    boost::this_thread::sleep(boost::posix_time::seconds(3) );
    qt.Stop();
}

Viatchesla...@h-d-gmbh.de

unread,
Apr 15, 2011, 9:18:06 AM4/15/11
to boost...@lists.boost.org
> Hi All, I have just started learning boost. I want my thread to support
> following functions
> Pause();
> Resume();
> Stop();.
> I have come up with following code, which ends up as dead lock. Can some
> one
> please guide me as how to achieve the desired functionality.
> Would really appreciate any help.
> Thanks.
> Kaz
> class qthread
> {
> public:
> boost::thread* mThread;
> boost::condition_variable mCond;
> boost::mutex mMutex;
> bool mExit;
> qthread(): mExit(false)
> {
> // create function pointer to local Execute function
> boost::function<void (qthread*)> fptr = &qthread::Execute;
> fptr(this);
you call Execute() here in the main thread


> // pass on function pointer along with this reference to boost
> thread
> boost::thread th0( fptr, this);
> // assign temp thread to local variable
> mThread = &th0;
> }

and now your th0 is destroyed leaving invalid pointer. Strange, I thought
gcc has already learned to warn about such stuff, but I don't get any
warning with gcc 4.3.2 here even with -Wall

> mCond.wait(lock); //<< DEAD LOCK
It's not a dead lock, don't swear with so scary words ;). If you'd debug
the program, you see you are still having only one thread by the time.

-- Slava

_______________________________________________
Boost-users mailing list
Boost...@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users

bit bit

unread,
Apr 15, 2011, 9:22:51 AM4/15/11
to boost...@lists.boost.org
remove fptr(this);
You call qthread's Execute() from your main thread and you are waiting on a condition variable.


> To: boost...@lists.boost.org
> From: Viatchesla...@h-d-gmbh.de
> Date: Fri, 15 Apr 2011 15:18:06 +0200
> Subject: Re: [Boost-users] [thread] : pause/resume thread

bit bit

unread,
Apr 15, 2011, 9:36:29 AM4/15/11
to boost...@lists.boost.org

inside qthread contructor, copy just the following and you are good to go.

mThread = new boost::thread(bind(&qthread::Execute, this));


Date: Fri, 15 Apr 2011 11:06:56 +0100
From: sfx...@googlemail.com
To: boost...@lists.boost.org
Subject: [Boost-users] [thread] : pause/resume thread

Kazz

unread,
Apr 15, 2011, 10:43:11 AM4/15/11
to boost...@lists.boost.org
>inside qthread contructor, copy just the following and you are good to go.
>mThread = new boost::thread(bind(&qthread::Execute, this));
 
Pardon my ignorance here, but I assume I also have to manually delete the mThread then ?,if not, when qThread object goes out of scope, who ll remove it, (or take its ownership) ?

2011/4/15 bit bit <g_ra...@hotmail.com>

bit bit

unread,
Apr 15, 2011, 3:10:20 PM4/15/11
to boost...@lists.boost.org
when qThread is destructed you will have to Join the thread and delete the pointer.


Date: Fri, 15 Apr 2011 15:43:11 +0100
From: sfx...@googlemail.com
To: boost...@lists.boost.org

Subject: Re: [Boost-users] [thread] : pause/resume thread

Reply all
Reply to author
Forward
0 new messages