Open Side Menu Go to the Top
Register
C++ Threading w/ Boost Library C++ Threading w/ Boost Library

03-05-2017 , 11:59 AM
Getting into more advanced topics like threading, and interprocess communication. I am looking for resources to develop my skills. Recommendations welcome.

I could not get this code to compile. The curly bracket usage is entirely alien to me.

Code:
#include <boost/thread.hpp>
#include <boost/chrono.hpp>
#include <iostream>

void wait(int seconds)
{
  boost::this_thread::sleep_for(boost::chrono::seconds{seconds});
}

void thread()
{
  for (int i = 0; i < 5; ++i)
  {
    wait(1);
    std::cout << i << '\n';
  }
}

int main()
{
  boost::thread t{thread};
  t.join();
}
https://theboostcpplibraries.com/boo...ead-management

C++ Threading w/ Boost Library Quote
03-05-2017 , 12:50 PM
And, yes, Boost library is installed successfully.
C++ Threading w/ Boost Library Quote
03-05-2017 , 02:39 PM
Well, your code is identical to the example on their website.

So, what's the command line you're compiling it with, and what is the error you get while compiling?
C++ Threading w/ Boost Library Quote
03-05-2017 , 05:30 PM
Code:
g++ thread.cpp -o thread.o
Using Linux Ubuntu.

Quote:
Originally Posted by error
thread.cpp:21:25: error: in C++98 ‘t’ must be initialized by constructor, not by ‘{...}’
boost::thread t{thread};
I have never see the curly fries used like this in C++. I guess it's possible with operator overloading. I copy/pasted the code verbatim, and that's the error I got. I purchased their book, so we'll see if it's any good or not.

Last edited by leavesofliberty; 03-05-2017 at 05:53 PM.
C++ Threading w/ Boost Library Quote
03-05-2017 , 05:59 PM
my guess is that you need to be using c++11 and you're not.

Try this first
g++ -std=c++11 thread.cpp -o thread.o

and if that doesn't work
g++ -std=c++0x thread.cpp -o thread.o
C++ Threading w/ Boost Library Quote
03-05-2017 , 06:11 PM
Just gave it a shot

Here's the big wall of text
Code:
thread.cpp: In function ‘void wait(int)’:
thread.cpp:7:48: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11 [enabled by default]
   boost::this_thread::sleep_for(boost::chrono::seconds{seconds});
                                                ^
/tmp/cc7m02Rt.o: In function `__static_initialization_and_destruction_0(int, int)':
thread.cpp:(.text+0x9e): undefined reference to `boost::system::generic_category()'
thread.cpp:(.text+0xaa): undefined reference to `boost::system::generic_category()'
thread.cpp:(.text+0xb6): undefined reference to `boost::system::system_category()'
/tmp/cc7m02Rt.o: In function `boost::this_thread::sleep_for(boost::chrono::duration<long, boost::ratio<1l, 1000000000l> > const&)':
thread.cpp:(.text._ZN5boost11this_thread9sleep_forERKNS_6chrono8durationIlNS_5ratioILl1ELl1000000000EEEEE[_ZN5boost11this_thread9sleep_forERKNS_6chrono8durationIlNS_5ratioILl1ELl1000000000EEEEE]+0x28): undefined reference to `boost::this_thread::hidden::sleep_for(timespec const&)'
collect2: error: ld returned 1 exit status
ubuntu@ubuntu:/var/cpp/programs/test/boost$ sudo g++ -std=c++11 thread.cpp -o thread.o
/tmp/ccjzS76y.o: In function `__static_initialization_and_destruction_0(int, int)':
thread.cpp:(.text+0x9f): undefined reference to `boost::system::generic_category()'
thread.cpp:(.text+0xab): undefined reference to `boost::system::generic_category()'
thread.cpp:(.text+0xb7): undefined reference to `boost::system::system_category()'
/tmp/ccjzS76y.o: In function `boost::this_thread::sleep_for(boost::chrono::duration<long, boost::ratio<1l, 1000000000l> > const&)':
thread.cpp:(.text._ZN5boost11this_thread9sleep_forERKNS_6chrono8durationIlNS_5ratioILl1ELl1000000000EEEEE[_ZN5boost11this_thread9sleep_forERKNS_6chrono8durationIlNS_5ratioILl1ELl1000000000EEEEE]+0x28): undefined reference to `boost::this_thread::hidden::sleep_for(timespec const&)'
collect2: error: ld returned 1 exit status
ubuntu@ubuntu:/var/cpp/programs/test/boost$ sudo nano thread.cppubuntu@ubuntu:/var/cpp/programs/test/boost$ sudo g++ -std=c++11 thread.cpp -o thread.o
/tmp/ccjM2RCL.o: In function `__static_initialization_and_destruction_0(int, int)':
thread.cpp:(.text+0xee): undefined reference to `boost::system::generic_category()'
thread.cpp:(.text+0xfa): undefined reference to `boost::system::generic_category()'
thread.cpp:(.text+0x106): undefined reference to `boost::system::system_category()'
/tmp/ccjM2RCL.o: In function `boost::thread_exception::thread_exception(int, char const*)':
thread.cpp:(.text._ZN5boost16thread_exceptionC2EiPKc[_ZN5boost16thread_exceptionC5EiPKc]+0x14): undefined reference to `boost::system::system_category()'
/tmp/ccjM2RCL.o: In function `boost::detail::thread_data_base::thread_data_base()':
thread.cpp:(.text._ZN5boost6detail16thread_data_baseC2Ev[_ZN5boost6detail16thread_data_baseC5Ev]+0x24): undefined reference to `vtable for boost::detail::thread_data_base'
/tmp/ccjM2RCL.o: In function `boost::this_thread::sleep_for(boost::chrono::duration<long, boost::ratio<1l, 1000000000l> > const&)':
thread.cpp:(.text._ZN5boost11this_thread9sleep_forERKNS_6chrono8durationIlNS_5ratioILl1ELl1000000000EEEEE[_ZN5boost11this_thread9sleep_forERKNS_6chrono8durationIlNS_5ratioILl1ELl1000000000EEEEE]+0x28): undefined reference to `boost::this_thread::hidden::sleep_for(timespec const&)'
/tmp/ccjM2RCL.o: In function `boost::thread::start_thread()':
thread.cpp:(.text._ZN5boost6thread12start_threadEv[_ZN5boost6thread12start_threadEv]+0x15): undefined reference to `boost::thread::start_thread_noexcept()'
/tmp/ccjM2RCL.o: In function `boost::thread::~thread()':
thread.cpp:(.text._ZN5boost6threadD2Ev[_ZN5boost6threadD5Ev]+0x14): undefined reference to `boost::thread::detach()'
/tmp/ccjM2RCL.o: In function `boost::thread::get_id() const':
thread.cpp:(.text._ZNK5boost6thread6get_idEv[_ZNK5boost6thread6get_idEv]+0x18): undefined reference to `boost::thread::native_handle()'
/tmp/ccjM2RCL.o: In function `boost::thread::join()':
thread.cpp:(.text._ZN5boost6thread4joinEv[_ZN5boost6thread4joinEv]+0x6d): undefined reference to `boost::thread::join_noexcept()'
/tmp/ccjM2RCL.o: In function `boost::detail::thread_data<void (*)()>::~thread_data()':
thread.cpp:(.text._ZN5boost6detail11thread_dataIPFvvEED2Ev[_ZN5boost6detail11thread_dataIPFvvEED5Ev]+0x1f): undefined reference to `boost::detail::thread_data_base::~thread_data_base()'
/tmp/ccjM2RCL.o:(.rodata._ZTIN5boost6detail11thread_dataIPFvvEEE[_ZTIN5boost6detail11thread_dataIPFvvEEE]+0x10): undefined reference to `typeinfo for boost::detail::thread_data_base'
collect2: error: ld returned 1 exit status
ubuntu@ubuntu:/var/cpp/programs/test/boost$ sudo nano thread.cppubuntu@ubuntu:/var/cpp/programs/test/boost$ sudo g++ -std=c++11 thread.cpp -o thread.o
/tmp/ccfDqou8.o: In function `__static_initialization_and_destruction_0(int, int)':
thread.cpp:(.text+0xee): undefined reference to `boost::system::generic_category()'
thread.cpp:(.text+0xfa): undefined reference to `boost::system::generic_category()'
thread.cpp:(.text+0x106): undefined reference to `boost::system::system_category()'
/tmp/ccfDqou8.o: In function `boost::thread_exception::thread_exception(int, char const*)':
thread.cpp:(.text._ZN5boost16thread_exceptionC2EiPKc[_ZN5boost16thread_exceptionC5EiPKc]+0x14): undefined reference to `boost::system::system_category()'
/tmp/ccfDqou8.o: In function `boost::detail::thread_data_base::thread_data_base()':
thread.cpp:(.text._ZN5boost6detail16thread_data_baseC2Ev[_ZN5boost6detail16thread_data_baseC5Ev]+0x24): undefined reference to `vtable for boost::detail::thread_data_base'
/tmp/ccfDqou8.o: In function `boost::this_thread::sleep_for(boost::chrono::duration<long, boost::ratio<1l, 1000000000l> > const&)':
thread.cpp:(.text._ZN5boost11this_thread9sleep_forERKNS_6chrono8durationIlNS_5ratioILl1ELl1000000000EEEEE[_ZN5boost11this_thread9sleep_forERKNS_6chrono8durationIlNS_5ratioILl1ELl1000000000EEEEE]+0x28): undefined reference to `boost::this_thread::hidden::sleep_for(timespec const&)'
/tmp/ccfDqou8.o: In function `boost::thread::start_thread()':
thread.cpp:(.text._ZN5boost6thread12start_threadEv[_ZN5boost6thread12start_threadEv]+0x15): undefined reference to `boost::thread::start_thread_noexcept()'
/tmp/ccfDqou8.o: In function `boost::thread::~thread()':
thread.cpp:(.text._ZN5boost6threadD2Ev[_ZN5boost6threadD5Ev]+0x14): undefined reference to `boost::thread::detach()'
/tmp/ccfDqou8.o: In function `boost::thread::get_id() const':
thread.cpp:(.text._ZNK5boost6thread6get_idEv[_ZNK5boost6thread6get_idEv]+0x18): undefined reference to `boost::thread::native_handle()'
/tmp/ccfDqou8.o: In function `boost::thread::join()':
thread.cpp:(.text._ZN5boost6thread4joinEv[_ZN5boost6thread4joinEv]+0x6d): undefined reference to `boost::thread::join_noexcept()'
/tmp/ccfDqou8.o: In function `boost::detail::thread_data<void (*)()>::~thread_data()':
thread.cpp:(.text._ZN5boost6detail11thread_dataIPFvvEED2Ev[_ZN5boost6detail11thread_dataIPFvvEED5Ev]+0x1f): undefined reference to `boost::detail::thread_data_base::~thread_data_base()'
/tmp/ccfDqou8.o:(.rodata._ZTIN5boost6detail11thread_dataIPFvvEEE[_ZTIN5boost6detail11thread_dataIPFvvEEE]+0x10): undefined reference to `typeinfo for boost::detail::thread_data_base'
collect2: error: ld returned 1 exit status
and

Code:
/tmp/ccip21sJ.o: In function `__static_initialization_and_destruction_0(int, int)':
thread.cpp:(.text+0xee): undefined reference to `boost::system::generic_category()'
thread.cpp:(.text+0xfa): undefined reference to `boost::system::generic_category()'
thread.cpp:(.text+0x106): undefined reference to `boost::system::system_category()'
/tmp/ccip21sJ.o: In function `boost::thread_exception::thread_exception(int, char const*)':
thread.cpp:(.text._ZN5boost16thread_exceptionC2EiPKc[_ZN5boost16thread_exceptionC5EiPKc]+0x14): undefined reference to `boost::system::system_category()'
/tmp/ccip21sJ.o: In function `boost::detail::thread_data_base::thread_data_base()':
thread.cpp:(.text._ZN5boost6detail16thread_data_baseC2Ev[_ZN5boost6detail16thread_data_baseC5Ev]+0x24): undefined reference to `vtable for boost::detail::thread_data_base'
/tmp/ccip21sJ.o: In function `boost::this_thread::sleep_for(boost::chrono::duration<long, boost::ratio<1l, 1000000000l> > const&)':
thread.cpp:(.text._ZN5boost11this_thread9sleep_forERKNS_6chrono8durationIlNS_5ratioILl1ELl1000000000EEEEE[_ZN5boost11this_thread9sleep_forERKNS_6chrono8durationIlNS_5ratioILl1ELl1000000000EEEEE]+0x28): undefined reference to `boost::this_thread::hidden::sleep_for(timespec const&)'
/tmp/ccip21sJ.o: In function `boost::thread::start_thread()':
thread.cpp:(.text._ZN5boost6thread12start_threadEv[_ZN5boost6thread12start_threadEv]+0x15): undefined reference to `boost::thread::start_thread_noexcept()'
/tmp/ccip21sJ.o: In function `boost::thread::~thread()':
thread.cpp:(.text._ZN5boost6threadD2Ev[_ZN5boost6threadD5Ev]+0x14): undefined reference to `boost::thread::detach()'
/tmp/ccip21sJ.o: In function `boost::thread::get_id() const':
thread.cpp:(.text._ZNK5boost6thread6get_idEv[_ZNK5boost6thread6get_idEv]+0x18): undefined reference to `boost::thread::native_handle()'
/tmp/ccip21sJ.o: In function `boost::thread::join()':
thread.cpp:(.text._ZN5boost6thread4joinEv[_ZN5boost6thread4joinEv]+0x6d): undefined reference to `boost::thread::join_noexcept()'
/tmp/ccip21sJ.o: In function `boost::detail::thread_data<void (*)()>::~thread_data()':
thread.cpp:(.text._ZN5boost6detail11thread_dataIPFvvEED2Ev[_ZN5boost6detail11thread_dataIPFvvEED5Ev]+0x1f): undefined reference to `boost::detail::thread_data_base::~thread_data_base()'
/tmp/ccip21sJ.o:(.rodata._ZTIN5boost6detail11thread_dataIPFvvEEE[_ZTIN5boost6detail11thread_dataIPFvvEEE]+0x10): undefined reference to `typeinfo for boost::detail::thread_data_base'
collect2: error: ld returned 1 exit status
I am wondering if boost is installed properly at this point. I believe it is. This test code I got from boost.org worked first try:

Code:
#include <boost/lambda/lambda.hpp>
#include <iostream>
#include <iterator>
#include <algorithm>

int main()
{
    using namespace boost::lambda;
    typedef std::istream_iterator<int> in;

    std::for_each(
        in(std::cin), in(), std::cout << (_1 * 3) << " " );
}
C++ Threading w/ Boost Library Quote
03-05-2017 , 06:30 PM
OK you're closer. Now you need to link with the boost library. If it's installed in a standard place (like /usr/local/lib) then you just need to add
-l boost_thread
to the command line that worked for you above.

Your sample that worked for you is probably a header-only library, with no compiled part, which is why it worked.

If your boost library is NOT in a standard place then you'll need another option. I guess take a look at, say
/usr/local/lib/libbost*
and see what's there. Unless you chose some other place to install it, that should be the default.
C++ Threading w/ Boost Library Quote
03-05-2017 , 06:41 PM
Thanks for replies. It's all installed in the standard spot. I verified it as follows.

Code:
ls /usr/local/lib/libboost*
Results:

Code:
/usr/local/lib/libboost_atomic.a
/usr/local/lib/libboost_atomic.so
/usr/local/lib/libboost_atomic.so.1.62.0
/usr/local/lib/libboost_chrono.a
/usr/local/lib/libboost_chrono.so
/usr/local/lib/libboost_chrono.so.1.62.0
/usr/local/lib/libboost_container.a
/usr/local/lib/libboost_container.so
/usr/local/lib/libboost_container.so.1.62.0
/usr/local/lib/libboost_context.a
/usr/local/lib/libboost_context.so
/usr/local/lib/libboost_context.so.1.62.0
/usr/local/lib/libboost_coroutine.a
/usr/local/lib/libboost_coroutine.so
/usr/local/lib/libboost_coroutine.so.1.62.0
/usr/local/lib/libboost_date_time.a
/usr/local/lib/libboost_date_time.so
/usr/local/lib/libboost_date_time.so.1.62.0
/usr/local/lib/libboost_exception.a
/usr/local/lib/libboost_filesystem.a
/usr/local/lib/libboost_filesystem.so
/usr/local/lib/libboost_filesystem.so.1.62.0
/usr/local/lib/libboost_graph.a
/usr/local/lib/libboost_graph.so
/usr/local/lib/libboost_graph.so.1.62.0
/usr/local/lib/libboost_iostreams.a
/usr/local/lib/libboost_iostreams.so
/usr/local/lib/libboost_iostreams.so.1.62.0
/usr/local/lib/libboost_locale.a
/usr/local/lib/libboost_locale.so
/usr/local/lib/libboost_locale.so.1.62.0
/usr/local/lib/libboost_log.a
/usr/local/lib/libboost_log_setup.a
/usr/local/lib/libboost_log_setup.so
/usr/local/lib/libboost_log_setup.so.1.62.0
/usr/local/lib/libboost_log.so
/usr/local/lib/libboost_log.so.1.62.0
/usr/local/lib/libboost_math_c99.a
/usr/local/lib/libboost_math_c99f.a
/usr/local/lib/libboost_math_c99f.so
/usr/local/lib/libboost_math_c99f.so.1.62.0
/usr/local/lib/libboost_math_c99l.a
/usr/local/lib/libboost_math_c99l.so
/usr/local/lib/libboost_math_c99l.so.1.62.0
/usr/local/lib/libboost_math_c99.so
/usr/local/lib/libboost_math_c99.so.1.62.0
/usr/local/lib/libboost_math_tr1.a
/usr/local/lib/libboost_math_tr1f.a
/usr/local/lib/libboost_math_tr1f.so
/usr/local/lib/libboost_math_tr1f.so.1.62.0
/usr/local/lib/libboost_math_tr1l.a
/usr/local/lib/libboost_math_tr1l.so
/usr/local/lib/libboost_math_tr1l.so.1.62.0
/usr/local/lib/libboost_math_tr1.so
/usr/local/lib/libboost_math_tr1.so.1.62.0
/usr/local/lib/libboost_prg_exec_monitor.a
/usr/local/lib/libboost_prg_exec_monitor.so
/usr/local/lib/libboost_prg_exec_monitor.so.1.62.0
/usr/local/lib/libboost_program_options.a
/usr/local/lib/libboost_program_options.so
/usr/local/lib/libboost_program_options.so.1.62.0
/usr/local/lib/libboost_python.a
/usr/local/lib/libboost_python.so
/usr/local/lib/libboost_python.so.1.62.0
/usr/local/lib/libboost_random.a
/usr/local/lib/libboost_random.so
/usr/local/lib/libboost_random.so.1.62.0
/usr/local/lib/libboost_regex.a
/usr/local/lib/libboost_regex.so
/usr/local/lib/libboost_regex.so.1.62.0
/usr/local/lib/libboost_serialization.a
/usr/local/lib/libboost_serialization.so
/usr/local/lib/libboost_serialization.so.1.62.0
/usr/local/lib/libboost_signals.a
/usr/local/lib/libboost_signals.so
/usr/local/lib/libboost_signals.so.1.62.0
/usr/local/lib/libboost_system.a
/usr/local/lib/libboost_system.so
/usr/local/lib/libboost_system.so.1.62.0
BUT, still not working.

Command

Code:
g++ -std=c++11 thread.cpp -o thread.o -l boost_thread
Result

Code:
/usr/bin/ld: /tmp/cchw9Dn2.o: undefined reference to symbol '_ZN5boost6system15system_categoryEv'
//usr/lib/x86_64-linux-gnu/libboost_system.so.1.54.0: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
Command

Code:
g++ -std=c++0x thread.cpp -o thread.o -l boost_thread
Result

Code:
/usr/bin/ld: /tmp/ccMNHy4b.o: undefined reference to symbol '_ZN5boost6system15system_categoryEv'
//usr/lib/x86_64-linux-gnu/libboost_system.so.1.54.0: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
Should I be using the linking flag before the output flag? Tried it, same error.
C++ Threading w/ Boost Library Quote
03-05-2017 , 06:47 PM
Well, if you look at that output there's no libboost_thread.so, which is the library you presumably want. So I guess that one did not get built/installed. I don't know if you installed Everything or just the defaults or what.
C++ Threading w/ Boost Library Quote
03-05-2017 , 06:53 PM
Quote:
Originally Posted by RustyBrooks
Well, if you look at that output there's no libboost_thread.so, which is the library you presumably want. So I guess that one did not get built/installed. I don't know if you installed Everything or just the defaults or what.
WAIT, it's there, just the code tag omitted it because it tl'dr.

Code:
/usr/local/lib/libboost_system.a
/usr/local/lib/libboost_system.so
/usr/local/lib/libboost_system.so.1.62.0
/usr/local/lib/libboost_test_exec_monitor.a
/usr/local/lib/libboost_thread.a
/usr/local/lib/libboost_thread.so
/usr/local/lib/libboost_thread.so.1.62.0
/usr/local/lib/libboost_timer.a
/usr/local/lib/libboost_timer.so
/usr/local/lib/libboost_timer.so.1.62.0
/usr/local/lib/libboost_type_erasure.a
/usr/local/lib/libboost_type_erasure.so
/usr/local/lib/libboost_type_erasure.so.1.62.0
/usr/local/lib/libboost_unit_test_framework.a
/usr/local/lib/libboost_unit_test_framework.so
/usr/local/lib/libboost_unit_test_framework.so.1.62.0
/usr/local/lib/libboost_wave.a
/usr/local/lib/libboost_wave.so
/usr/local/lib/libboost_wave.so.1.62.0
/usr/local/lib/libboost_wserialization.a
/usr/local/lib/libboost_wserialization.so
/usr/local/lib/libboost_wserialization.so.1.62.0
I thought I did a full install of all packages.
C++ Threading w/ Boost Library Quote
03-05-2017 , 07:45 PM
Found the error on StackOverflow

http://stackoverflow.com/questions/2...m-command-line

and I tried to add -lpthread there, but it did not work.
C++ Threading w/ Boost Library Quote
03-05-2017 , 07:56 PM
You need to add "-lboost_system" to the command line.

If you have a C++11 compiler, it's probably better to use C++11's thread instead of boost::thread though (note: you will still need to link with "-lpthread" for C++11 threads).

Juk
C++ Threading w/ Boost Library Quote
03-05-2017 , 08:02 PM
Thanks Juk, getting closer.

Code:
g++ -std=c++11 thread.cpp -l boost_thread -lboost_system -lpthread -o thread.o
Code:
/tmp/ccNBMuLb.o: In function `boost::this_thread::sleep_for(boost::chrono::duration<long, boost::ratio<1l, 1000000000l> > const&)':
thread.cpp:(.text._ZN5boost11this_thread9sleep_forERKNS_6chrono8durationIlNS_5ratioILl1ELl1000000000EEEEE[_ZN5boost11this_thread9sleep_forERKNS_6chrono8durationIlNS_5ratioILl1ELl1000000000EEEEE]+0x28): undefined reference to `boost::this_thread::hidden::sleep_for(timespec const&)'
collect2: error: ld returned 1 exit status
C++ Threading w/ Boost Library Quote
03-05-2017 , 08:04 PM
Try "-lboost_thread" instead of "-l boost_thread".

Juk
C++ Threading w/ Boost Library Quote
03-05-2017 , 08:05 PM
Also try adding "lboost_chrono" and see if that helps.

Juk
C++ Threading w/ Boost Library Quote
03-05-2017 , 08:10 PM
Quote:
Originally Posted by jukofyork
Also try adding "lboost_chrono" and see if that helps.

Juk
Still no workie

Code:
g++ -std=c++11 thread.cpp -l boost_thread -lboost_system -lboost_chrono -lpthread -o thread.o
Result

Code:
/tmp/cc8J8B3P.o: In function `boost::this_thread::sleep_for(boost::chrono::duration<long, boost::ratio<1l, 1000000000l> > const&)':
thread.cpp:(.text._ZN5boost11this_thread9sleep_forERKNS_6chrono8durationIlNS_5ratioILl1ELl1000000000EEEEE[_ZN5boost11this_thread9sleep_forERKNS_6chrono8durationIlNS_5ratioILl1ELl1000000000EEEEE]+0x28): undefined reference to `boost::this_thread::hidden::sleep_for(timespec const&)'
collect2: error: ld returned 1 exit status
But, at least I learned now I have to link the libraries that I'm using with Boost.

Also, changed to -lboost_thread

Code:
g++ -std=c++11 thread.cpp -lboost_thread -lboost_system -lboost_chrono -lpthread -o thread.o
, no dice.
Code:
/tmp/cc7138xS.o: In function `boost::this_thread::sleep_for(boost::chrono::duration<long, boost::ratio<1l, 1000000000l> > const&)':
thread.cpp:(.text._ZN5boost11this_thread9sleep_forERKNS_6chrono8durationIlNS_5ratioILl1ELl1000000000EEEEE[_ZN5boost11this_thread9sleep_forERKNS_6chrono8durationIlNS_5ratioILl1ELl1000000000EEEEE]+0x28): undefined reference to `boost::this_thread::hidden::sleep_for(timespec const&)'
collect2: error: ld returned 1 exit status
Seems like we fixed some of it, and the error is in the sleep command. I'll Google, and see what comes up.
C++ Threading w/ Boost Library Quote
03-05-2017 , 08:16 PM
Try:
Code:
g++ -std=c++11 thread.cpp -lpthread -lboost_system -lboost_chrono -lboost_thread -o thread.o
Juk
C++ Threading w/ Boost Library Quote
03-05-2017 , 08:21 PM
Yeah, annoyingly, the order of the libraries matters.

And I actually agree that you should probably just use c++11 threads.
C++ Threading w/ Boost Library Quote
03-05-2017 , 08:24 PM
Quote:
Originally Posted by jukofyork
Try:
Code:
g++ -std=c++11 thread.cpp -lpthread -lboost_system -lboost_chrono -lboost_thread -o thread.o
Juk
Code:
g++ -std=c++11 thread.cpp -lpthread -lboost_system -lboost_chrono -lboost_thread -o thread.o


Spoiler:

Code:
/tmp/ccjeNxCO.o: In function `boost::this_thread::sleep_for(boost::chrono::duration<long, boost::ratio<1l, 1000000000l> > const&)':
thread.cpp:(.text._ZN5boost11this_thread9sleep_forERKNS_6chrono8durationIlNS_5ratioILl1ELl1000000000EEEEE[_ZN5boost11this_thread9sleep_forERKNS_6chrono8durationIlNS_5ratioILl1ELl1000000000EEEEE]+0x28): undefined reference to `boost::this_thread::hidden::sleep_for(timespec const&)'
collect2: error: ld returned 1 exit status
Spoiler:



C++ Threading w/ Boost Library Quote
03-05-2017 , 08:28 PM
Quote:
Originally Posted by leavesofliberty
Seems like we fixed some of it, and the error is in the sleep command. I'll Google, and see what comes up.
It seems to be something to do with pthreads:

Code:
boost::this_thread::hidden::sleep_for(timespec const&)
as that isn't the function you are calling directly (with a "chrono" time), but something boost is calling to offload the work to the pthreads library.

Did you get any errors mentioning pthreads when you built the boost libraries?

Juk
C++ Threading w/ Boost Library Quote
03-05-2017 , 08:37 PM
Reading this: http://www.boost.org/doc/libs/1_63_0...ead/build.html

it looks like you might also need the Boost.DateTime library:

Quote:
Boost.DateTime

The Boost.DateTime time related functions introduced in Boost 1.35.0, using the Boost.Date_Time library are deprecated. These include (but are not limited to):

boost::this_thread::sleep()
.
.
.
So try adding "-lboost_date_time" to the command line and see if that helps.

Juk

Last edited by jukofyork; 03-05-2017 at 08:39 PM. Reason: -lboost_date_time not -boost_date_time
C++ Threading w/ Boost Library Quote
03-05-2017 , 08:47 PM
Quote:
Originally Posted by jukofyork
Reading this: http://www.boost.org/doc/libs/1_63_0...ead/build.html

it looks like you might also need the Boost.DateTime library:



So try adding "-lboost_date_time" to the command line and see if that helps.

Juk
Thx, tried it

Code:
g++ -std=c++11 thread.cpp -lboost_date_time -lpthread -lboost_system -lboost_chrono -lboost_thread -o thread.o
Spoiler:

Quote:
Originally Posted by Ben Stein
It did not work and the US sank deeper into the Great Depression


Code:
/tmp/ccDlgmQi.o: In function `boost::this_thread::sleep_for(boost::chrono::duration<long, boost::ratio<1l, 1000000000l> > const&)':
thread.cpp:(.text._ZN5boost11this_thread9sleep_forERKNS_6chrono8durationIlNS_5ratioILl1ELl1000000000EEEEE[_ZN5boost11this_thread9sleep_forERKNS_6chrono8durationIlNS_5ratioILl1ELl1000000000EEEEE]+0x28): undefined reference to `boost::this_thread::hidden::sleep_for(timespec const&)'
collect2: error: ld returned 1 exit status
I could just try to link the whole library. I love the concept of Boost, but forgot what a pain linking can be.
C++ Threading w/ Boost Library Quote
03-05-2017 , 10:29 PM
Quote:
Originally Posted by jukofyork
It seems to be something to do with pthreads:

Code:
boost::this_thread::hidden::sleep_for(timespec const&)
as that isn't the function you are calling directly (with a "chrono" time), but something boost is calling to offload the work to the pthreads library.

Did you get any errors mentioning pthreads when you built the boost libraries?

Juk
Missed your post. I'll look into pthreads.
C++ Threading w/ Boost Library Quote
03-06-2017 , 01:51 AM
I decided to uninstall Boost, and re-install it, and now it works!

C++ Threading w/ Boost Library Quote
03-06-2017 , 04:50 AM
We've been trying to extricate ourselves of Boost for awhile at work, and boy is it messy. We use just one small part of it yet despite that, I think someone discovered it's the #1 contributor of header includes to our project (like, by including a billion of other boost headers from within its own headers).

As someone who just arrived on the "modern C++" scene a couple years ago, lots of Boost stuff (including threads) seems to me like it's been assimilated into or superseded by C++11.
C++ Threading w/ Boost Library Quote

      
m