Opened 10 years ago

Closed 10 years ago

#7238 closed Bugs (fixed)

this_thread::sleep_for() does not respond to interrupt()

Reported by: CSB Owned by: viboes
Milestone: Boost 1.52.0 Component: thread
Version: Boost 1.50.0 Severity: Problem
Keywords: thread sleep_for interrupt Cc:

Description

Calling the interrupt() member of a thread has no effect when the thread calls this_thread::sleep_for(). The trivial program used to test this did exhibit the correct behaviour for this_thread::sleep_until() and this_thread::sleep().

Test run on CentOS 6.3

Attachments (1)

7238.patch (1.7 KB ) - added by viboes 10 years ago.
Please, could you try this patch?

Download all attachments as: .zip

Change History (7)

comment:1 by Nick F, 10 years ago

I am also observing the issue on Ubuntu 12.04. See the test reproducer below.

#include <iostream>
#include <boost/thread.hpp>

using namespace boost;
using namespace boost::chrono;

void f()
{
    try
    {
        std::cout << "Starting sleep in thread" << std::endl;
        while(true)
        {
            this_thread::sleep_for(seconds(60));
        }
    }
    catch(const thread_interrupted&)
    {
        std::cout << "Thread interrupted." << std::endl;
    }
}

int main(int argc, char** argv)
{
    thread t(f);
    t.interrupt();
    t.join();
    std::cout << "Joined with thread." << std::endl;
    return 0;
}

The problem seems to be due to the following. On platforms where BOOST_HAS_NANOSLEEP is defined, a nanosleep-based implementation is used for sleep_for (see thread/src/pthread/thread.cpp), which presumably does not allow the interruption mechanisms to work. Commenting out the BOOST_HAS_NANOSLEEP section in the above file and allowing the condition_variable based implementation to be used causes the above example to run as expected.

comment:2 by viboes, 10 years ago

Severity: ShowstopperProblem

Thanks for catching this.

I don't think this is a Showstopper as you can add a call to this_thread::interruption_point() together with the call to sleep_for, isn't it?

in reply to:  2 comment:3 by viboes, 10 years ago

Replying to viboes:

Thanks for catching this.

I don't think this is a Showstopper as you can add a call to this_thread::interruption_point() together with the call to sleep_for, isn't it?

Forget the last comment.

by viboes, 10 years ago

Attachment: 7238.patch added

Please, could you try this patch?

comment:4 by viboes, 10 years ago

Milestone: To Be DeterminedBoost 1.52.0

Committed in trunk revision 80076.

comment:5 by viboes, 10 years ago

Owner: changed from Anthony Williams to viboes
Status: newassigned

comment:6 by viboes, 10 years ago

Resolution: fixed
Status: assignedclosed

Merged to release [80450].

Note: See TracTickets for help on using tickets.