Modify

Opened 10 months ago

Closed 5 months ago

#12949 closed Bugs (fixed)

using sleep_for in a thread context without including boost/thread/thread.hpp yields incorrect behaviour when BOOST_THREAD_HAS_CONDATTR_SET_CLOCK_MONOTONIC is defined

Reported by: rkaris@… Owned by: viboes
Milestone: Boost 1.66.0 Component: thread
Version: Boost 1.63.0 Severity: Problem
Keywords: Cc:

Description

When defining a function f() in a compilation unit which uses boost::thread::sleep_for() which does not include <boost/thread/thread.hpp>, but another boost thread header,

function.h

#ifndef FUNCTION_H
#define FUNCTION_H

void f();

#endif

function.cpp:

#define BOOST_THREAD_VERSION 4
#define BOOST_THREAD_HAS_CONDATTR_SET_CLOCK_MONOTONIC

#include <boost/thread/condition_variable.hpp>

#include "function.h"

void f()
{
    boost::this_thread::sleep_for(boost::chrono::milliseconds(10));
}

and defining a compilation unit which uses this function in a thread context,

main.cpp:

#define BOOST_THREAD_VERSION 4
#define BOOST_THREAD_HAS_CONDATTR_SET_CLOCK_MONOTONIC

#include "function.h"
#include <boost/thread.hpp>

int main()
{
    auto t = boost::thread([]
    {
        f();
    });

    t.join();

    return 0;
}

the sleep_for() waits much long than expected when a monotonic clock is used.

This has been tested with both boost versions 1.63.0 and 1.64.0 beta2.

Attachments (0)

Change History (10)

comment:1 Changed 9 months ago by John Maddock

Component: Nonethread
Owner: set to Anthony Williams

comment:2 Changed 9 months ago by viboes

This

#define BOOST_THREAD_VERSION 4
#define BOOST_THREAD_HAS_CONDATTR_SET_CLOCK_MONOTONIC

#include <boost/thread/condition_variable.hpp>

#include "function.h"

void f()
{
    boost::this_thread::sleep_for(boost::chrono::milliseconds(10)); // **
}

shouldn't compile. You need to include

<boost/thread/thread.hpp>

I will why it compiles.

Are you testing on Windows?

comment:3 Changed 9 months ago by viboes

Owner: changed from Anthony Williams to viboes
Status: newassigned

comment:4 Changed 9 months ago by viboes

Could you be more specific about what incorrect behavior do you get. What is the difference in time?

comment:6 Changed 9 months ago by rkaris@…

I'm testing on linux in this case. It seems the sleep_for() in the example selects the sleep_for() nanoseconds overload found in boost/thread/pthread/thread_data.hpp instead of the boost/thread/v2/thread.hpp one. With the monotonic clock defined, this seems to lead to a very long sleeping period.

comment:7 Changed 5 months ago by viboes

Sorry for the late response.

Please could you try adding

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

in <boost/thread/pthread/condition_variable.hpp>

#if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
#include <boost/thread/pthread/thread_data.hpp>
#include <boost/thread/v2/thread.hpp>
#endif

comment:8 Changed 5 months ago by viboes

I have another idea. What about provoking a linker error

Undefined symbols for architecture x86_64:
  "void boost::this_thread::sleep_for<long long, boost::ratio<1l, 1000l> >(boost::chrono::duration<long long, boost::ratio<1l, 1000l> > const&)", referenced from:

comment:9 Changed 5 months ago by viboes

Milestone: To Be DeterminedBoost 1.66.0

comment:10 Changed 5 months ago by viboes

Resolution: fixed
Status: assignedclosed

Modify Ticket

Change Properties
Set your email in Preferences
Action
as closed The owner will remain viboes.
The resolution will be deleted.

Add Comment


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

 
Note: See TracTickets for help on using tickets.