[Thread] this_tread::sleep won't compile with gcc, "no match for operator+"

Hi! This is most probably a "newbie question", but I really don't see what exactly wrong I'm doing, so will appreciate any inputs. Basically, what I'm trying to do is: using namespace boost::posix_time; using namespace boost::this_thread; time_duration td = milliseconds(2000); sleep(&td); And it works ok with Visual Studio and Windows. Once I get it to linux and try to compile with gcc, this is what I get: g++ -c -Wall -I/home/gsed/boost/ main.cpp -o main.o In file included from /home/gsed/boost/boost/thread/thread.hpp:17:0, from includes.h:12, from main.cpp:1: /home/gsed/boost/boost/thread/pthread/thread_data.hpp: In function void boost::this_thread::sleep(const TimeDuration&) [with TimeDuration = boost::posix_time::time_duration*]: main.cpp:47:11: instantiated from here /home/gsed/boost/boost/thread/pthread/thread_data.hpp:138:13: error: no match for operator+ in boost::get_system_time() + rel_time /home/gsed/boost/boost/date_time/time.hpp:139:15: note: candidates are: time_type boost::date_time::base_time<T, time_system>::operator+(boost::date_time::base_time<T, time_system>::date_duration_type&) const [with T = boost::posix_time::ptime, time_system = boost::date_time::counted_time_system<boost::date_time::counted_time_rep<boost::posix_time::millisec_posix_time_system_config>
, time_type = boost::posix_time::ptime, boost::date_time::base_time<T, time_system>::date_duration_type = boost::gregorian::date_duration] /home/gsed/boost/boost/date_time/time.hpp:159:15: note: time_type boost::date_time::base_time<T, time_system>::operator+(boost::date_time::base_time<T, time_system>::time_duration_type&) const [with T = boost::posix_time::ptime, time_system = boost::date_time::counted_time_system<boost::date_time::counted_time_rep<boost::posix_time::millisec_posix_time_system_config> , time_type = boost::posix_time::ptime, boost::date_time::base_time<T, time_system>::time_duration_type = boost::posix_time::time_duration] /home/gsed/boost/boost/date_time/posix_time/date_duration_operators.hpp:75:3: note: boost::posix_time::ptime boost::posix_time::operator+(const boost::posix_time::ptime&, const boost::gregorian::years&) /home/gsed/boost/boost/date_time/posix_time/date_duration_operators.hpp:31:3: note: boost::posix_time::ptime boost::posix_time::operator+(const boost::posix_time::ptime&, const boost::gregorian::months&) make: *** [main.o] Error 1
ie it's trying to perform a boost::posix_time::ptime + time_duration operation, but for some reason can't decide between operator+(boost::date_time::base_time<T, time_system>::date_duration_type&) and operator+(boost::date_time::base_time<T, time_system>::time_duration_type&) And I'm absolutely not sure why. Win version works well I guess because there's another include file used and the whole thing is arranged differently. Again, any inputs or advice are greatly appreciated. Thanks! -- Take care, Alexey Malakhov

Basically, what I'm trying to do is:
using namespace boost::posix_time; using namespace boost::this_thread;
time_duration td = milliseconds(2000); sleep(&td);
Just curious: what's the purpose of the ampersand here? http://www.boost.org/doc/libs/1_46_1/doc/html/thread/thread_management.html#...

2011/5/30 Igor R <boost.lists@gmail.com>
Basically, what I'm trying to do is:
using namespace boost::posix_time; using namespace boost::this_thread;
time_duration td = milliseconds(2000); sleep(&td);
Just curious: what's the purpose of the ampersand here?
http://www.boost.org/doc/libs/1_46_1/doc/html/thread/thread_management.html#...
Igor, thanks for the note. My ampersand came from VS refusing to compile otherwise. But you're right, semantically it should not be there. And it's true, in win32 version this_thread::sleep() declaration is inline void sleep(TimeDuration const& rel_time) { interruptible_wait(detail::pin_to_zero(rel_time->total_milliseconds())); } TimeDuration=boost::posix_time::time_duration so rel_time->total_milliseconds() get a compiler error: type 'boost::posix_time::time_duration' does not have an overloaded member 'operator ->' D:\Projs\Libs\boost_1_46_1\boost/date_time/posix_time/posix_time_config.hpp(57) : see declaration of 'boost::posix_time::time_duration' did you intend to use '.' instead? This doesn't happen in linux version where sleep is inline void sleep(TimeDuration const& rel_time) { this_thread::sleep(get_system_time()+rel_time); } (this is the version that refused to work with boost::this_thread::sleep(&td) as in the root post) So right now, the code that works for both win32 and linux is using namespace boost::posix_time; using namespace boost::this_thread; time_duration td = milliseconds(2000); #ifdef _WIN32 sleep(&td); #else sleep(td); #endif It's probably not supposed to be like that, but this is the only way it's working for me. If anyone could come up with a more elegant solution, I'd appreciate it greatly. -- Take care, Alexey Malakhov

And it's true, in win32 version this_thread::sleep() declaration is inline void sleep(TimeDuration const& rel_time) {
interruptible_wait(detail::pin_to_zero(rel_time->total_milliseconds())); } TimeDuration=boost::posix_time::time_duration
so rel_time->total_milliseconds() get a compiler error:
I'm looking at this: http://www.boost.org/doc/libs/1_46_1/boost/thread/win32/thread_data.hpp ...and I don't see any "->" there. Do you work with the official 1.46.1 release, or with some dev trunk? HTH, Igor'.

Alexey Malakhov <malakhov@iki.rssi.ru> writes:
2011/5/30 Igor R <boost.lists@gmail.com>
> Basically, what I'm trying to do is: > > using namespace boost::posix_time; > using namespace boost::this_thread; > > time_duration td = milliseconds(2000); > sleep(&td);
Just curious: what's the purpose of the ampersand here? http://www.boost.org/doc/libs/1_46_1/doc/html/thread/thread_management.html #thread.thread_management.this_thread.sleep
Igor, thanks for the note. My ampersand came from VS refusing to compile otherwise. But you're right, semantically it should not be there.
And it's true, in win32 version this_thread::sleep() declaration is inline void sleep(TimeDuration const& rel_time) { interruptible_wait(detail::pin_to_zero(rel_time->total_milliseconds ())); }
No, it isn't. I don't know where the -> came from, but it's not in the boost source.
It's probably not supposed to be like that, but this is the only way it's working for me. If anyone could come up with a more elegant solution, I'd appreciate it greatly.
Install the latest boost and use sleep(td) on all platforms. Anthony -- Author of C++ Concurrency in Action http://www.stdthread.co.uk/book/ just::thread C++0x thread library http://www.stdthread.co.uk Just Software Solutions Ltd http://www.justsoftwaresolutions.co.uk 15 Carrallack Mews, St Just, Cornwall, TR19 7UL, UK. Company No. 5478976

No, it isn't. I don't know where the -> came from, but it's not in the boost source.
It's probably not supposed to be like that, but this is the only way it's working for me. If anyone could come up with a more elegant solution, I'd appreciate it greatly.
Install the latest boost and use sleep(td) on all platforms.
Anthony, thanks! It really has been something weird with my distrib, had to reinstall it from scratch. I must've messed up with it in some way for sure, but in any case - thank you and Igor for pointing this out.
-- Take care, Alexey Malakhov
participants (3)
-
Alexey Malakhov
-
Anthony Williams
-
Igor R