Opened 6 years ago

Closed 5 years 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.

Change History (10)

comment:1 by John Maddock, 6 years ago

Component: Nonethread
Owner: set to Anthony Williams

comment:2 by viboes, 5 years ago

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 by viboes, 5 years ago

Owner: changed from Anthony Williams to viboes
Status: newassigned

comment:4 by viboes, 5 years ago

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

comment:6 by rkaris@…, 5 years ago

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 by viboes, 5 years ago

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 by viboes, 5 years ago

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 by viboes, 5 years ago

Milestone: To Be DeterminedBoost 1.66.0
Note: See TracTickets for help on using tickets.