Unable to compile thread example on the document.

Hi, I cannot compile the thread example listed on the document: http://www.boost.org/libs/thread/doc/thread.html#Example I'm using g++ 2.95.3 on a FreeBSD 4.x box. Here are the exact environment and compile error messages: % uname -a FreeBSD XXX.foo.bar 4.5-PRERELEASE FreeBSD 4.5-PRERELEASE #0: Tue Jan 8 03:14:40 CST 2002 root@XXX.foo.bar:/usr/src/sys/compile/kernel.XXX i386 % g++ -v Using builtin specs. gcc version 2.95.3 20010315 (release) [FreeBSD] % cat test_thread0.cpp #include <boost/thread/xtime.hpp> #include <boost/thread/thread.hpp> #include <iostream> struct thread_alarm { thread_alarm(int secs) : m_secs(secs) { } void operator()() { boost::xtime xt; boost::xtime_get(&xt, ::boost::TIME_UTC); xt.sec += m_secs; boost::thread::sleep(xt); std::cout << "alarm sounded..." << std::endl; } int m_secs; }; void run() { int secs = 5; std::cout << "setting alarm for 5 seconds..." << std::endl; boost::thread thrd(thread_alarm(secs)); std::cout << "main()" << std::endl; thrd.join(); } int main() { run(); } % g++ -I/usr/local/include test_thread0.cpp test_thread0.cpp: In function `void run()': test_thread0.cpp:28: request for member `join' in `thrd', which is of non-aggregate type `boost::thread ()(thread_alarm)' The boost library is installed through FreeBSD's ports collection which the boost version is 1.26.0. I've tried to figure out where the problem is and failed to find the definition of class thread. Do I miss something? Or, is it just a freebsd porting problem? ps. The example seems lack of a xtime inclution. So I add the #include at the first line: #include <boost/thread/xtime.hpp> -- Chien-Chou Hung / Jeff Hung / 洪健洲 (Big5) National Chiao Tung University Institute of Information Management 交通大學資訊管理所碩士生 http://www.jeffhung.idv.tw/ mailto:jeffhung.iim90g@nctu.edu.tw

On 2/4/02 12:02 AM, jeffhung@ms44.url.com.tw wrote:
I cannot compile the thread example listed on the document: http://www.boost.org/libs/thread/doc/thread.html#Example
[...]
boost::thread thrd(thread_alarm(secs));
[...]
test_thread0.cpp: In function `void run()': test_thread0.cpp:28: request for member `join' in `thrd', which is of non-aggregate type `boost::thread ()(thread_alarm)'
I've tried to figure out where the problem is and failed to find the definition of class thread. Do I miss something? Or, is it just a freebsd porting problem?
This is a bug in the example that has been fixed for the next upcoming Boost release. The web site will be updated soon. The problem line above is can be interpreted as a declaration of a function named thrd with a parameter of type thread_alarm named secs, rather than a declaration of an object named thrd. And the C++ rule is that if it can be a function declaration, then it is. The fix is to replace the line with these two lines: thread_alarm alarm(secs); boost::thread thrd(alarm); Hope that helps. -- Darin
participants (2)
-
Darin Adler
-
御風只配做笨木頭