Modify

Opened 5 years ago

Closed 5 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 5 years ago.
Please, could you try this patch?

Download all attachments as: .zip

Change History (7)

comment:1 Changed 5 years 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: Changed 5 years 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 5 years 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 5 years ago by viboes

Please, could you try this patch?

comment:4 Changed 5 years ago by viboes

  • Milestone changed from To Be Determined to Boost 1.52.0

Committed in trunk revision 80076.

comment:5 Changed 5 years ago by viboes

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

comment:6 Changed 5 years ago by viboes

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

Merged to release [80450].

Add Comment

Modify Ticket

Change Properties
Set your email in Preferences
Action
as closed The owner will remain viboes.
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.