Modify

Ticket #7238 (closed Bugs: fixed)

Opened 20 months ago

Last modified 20 months ago

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

7238.patch Download (1.7 KB) - added by viboes 20 months ago.
Please, could you try this patch?

Change History

comment:1 Changed 20 months ago by Nick F

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 follow-up: ↓ 3 Changed 20 months ago by viboes

  • Severity changed from Showstopper to Problem

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?

comment:3 in reply to: ↑ 2 Changed 20 months ago by viboes

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.

Changed 20 months ago by viboes

Please, could you try this patch?

comment:4 Changed 20 months ago by viboes

  • Milestone changed from To Be Determined to Boost 1.52.0

Committed in trunk revision 80076.

comment:5 Changed 20 months ago by viboes

  • Owner changed from anthonyw to viboes
  • Status changed from new to assigned

comment:6 Changed 20 months ago by viboes

  • Status changed from assigned to closed
  • Resolution set to fixed

Merged to release [80450].

View

Add a comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
The resolution will be deleted. Next status will be 'reopened'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.