Boost logo

Boost Users :

Subject: [Boost-users] scoped_lock lock&unlock
From: weii (willgunx_at_[hidden])
Date: 2010-03-19 00:36:29


Dear all,

I got a compiler(gcc 3.4.5) error while using boost.thread:

-----------------------------------------------------------------------------
g++ -IC:\Program Files\boost\boost_1_40 -O0 -g3 -Wall -c
-fmessage-length=0 -omain.o ..\main.cpp
C:/Program
Files/boost/boost_1_40/boost/thread/win32/basic_timed_mutex.hpp: In
member function `bool boost::detail::basic_timed_mutex::timed_lock(const
Duration&) [with Duration = bool]':
C:/Program Files/boost/boost_1_40/boost/thread/locks.hpp:353:
instantiated from `bool boost::unique_lock<Mutex>::timed_lock(const
TimeDuration&) [with TimeDuration = bool, Mutex = boost::mutex]'
C:/Program Files/boost/boost_1_40/boost/thread/locks.hpp:241:
instantiated from `boost::unique_lock<Mutex>::unique_lock(Mutex&, const
TimeDuration&) [with TimeDuration = bool, Mutex = boost::mutex]'
..\main.cpp:17: instantiated from here
C:/Program
Files/boost/boost_1_40/boost/thread/win32/basic_timed_mutex.hpp:118:
error: no match for 'operator+' in 'boost::get_system_time() + timeout'
C:/Program Files/boost/boost_1_40/boost/date_time/time.hpp:140: note:
candidates are: T boost::date_time::base_time<T,
time_system>::operator+(const typename time_system::date_duration_type&)
const [with T = boost::posix_time::ptime, time_system =
boost::posix_time::posix_time_system]
C:/Program Files/boost/boost_1_40/boost/date_time/time.hpp:160: note:
               T boost::date_time::base_time<T,
time_system>::operator+(const typename time_system::time_duration_type&)
const [with T = boost::posix_time::ptime, time_system =
boost::posix_time::posix_time_system]
C:/Program Files/boost/boost_1_40/boost/operators.hpp:255: note:
 
boost::date_time::date_duration<boost::gregorian::date_duration_rep>
boost::operator+(const
boost::date_time::date_duration<boost::gregorian::date_duration_rep>&,
const boost::date_time::date_duration<boost::gregorian::date_duration_rep>&)
C:/Program
Files/boost/boost_1_40/boost/date_time/posix_time/date_duration_operators.hpp:76:
note: boost::posix_time::ptime
boost::posix_time::operator+(const boost::posix_time::ptime&, const
boost::gregorian::years&)
C:/Program
Files/boost/boost_1_40/boost/date_time/posix_time/date_duration_operators.hpp:32:
note: boost::posix_time::ptime
boost::posix_time::operator+(const boost::posix_time::ptime&, const
boost::gregorian::months&)
C:/Program
Files/boost/boost_1_40/boost/date_time/date_duration_types.hpp:245:
note: boost::gregorian::date
boost::date_time::operator+(const boost::gregorian::date&, const
boost::date_time::years_duration<boost::gregorian::greg_durations_config>&)
C:/Program
Files/boost/boost_1_40/boost/date_time/date_duration_types.hpp:133:
note: boost::gregorian::date
boost::date_time::operator+(const boost::gregorian::date&, const
boost::date_time::months_duration<boost::gregorian::greg_durations_config>&)
Build error occurred, build is stopped
-----------------------------------------------------------------------------------------------------------------------------

The following is my code:
-----------------------------------------------------------------------------------------------------------------------------
#include <boost/thread/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/bind.hpp>
#include <iostream>

boost::mutex io_mutex;

void count(int id)
{
        boost::mutex::scoped_lock lock(io_mutex,false);
        for (int i = 0; i < 10; ++i)
        {
                lock.lock();
                std::cout << id << ": " << i << std::endl;
                lock.unlock();
        }
}

int main(int argc, char* argv[])
{
        boost::thread thrd1(count, 1);
        boost::thread thrd2(count, 2);
        thrd1.join();
        thrd2.join();
        return 0;
}
-----------------------------------------------------------------------------
I know I can just abandon the lock() and unlock() function since the
constructor and destructor will do them for me.
But I can't understand how this error occurs and why the compiler has
something to do with basic_timed_mutex.

Anyone can give me some clues?
Thanks in advance.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net