Two Plus Two Publishing LLC Two Plus Two Publishing LLC
 

Go Back   Two Plus Two Poker Forums > >

Notices

Programming Discussions about computer programming

Reply
 
Thread Tools Display Modes
Old 03-05-2017, 11:59 AM   #1
leavesofliberty
Pooh-Bah
 
Join Date: Jul 2010
Posts: 5,765
C++ Threading w/ Boost Library

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

leavesofliberty is offline   Reply With Quote
Old 03-05-2017, 12:50 PM   #2
leavesofliberty
Pooh-Bah
 
Join Date: Jul 2010
Posts: 5,765
Re: C++ Threading w/ Boost Library

And, yes, Boost library is installed successfully.
leavesofliberty is offline   Reply With Quote
Old 03-05-2017, 02:39 PM   #3
RustyBrooks
Carpal \'Tunnel
 
RustyBrooks's Avatar
 
Join Date: Feb 2006
Location: Austin, TX
Posts: 23,124
Re: C++ Threading w/ Boost Library

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?
RustyBrooks is offline   Reply With Quote
Old 03-05-2017, 05:30 PM   #4
leavesofliberty
Pooh-Bah
 
Join Date: Jul 2010
Posts: 5,765
Re: C++ Threading w/ Boost Library

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.
leavesofliberty is offline   Reply With Quote
Old 03-05-2017, 05:59 PM   #5
RustyBrooks
Carpal \'Tunnel
 
RustyBrooks's Avatar
 
Join Date: Feb 2006
Location: Austin, TX
Posts: 23,124
Re: C++ Threading w/ Boost Library

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
RustyBrooks is offline   Reply With Quote
Old 03-05-2017, 06:11 PM   #6
leavesofliberty
Pooh-Bah
 
Join Date: Jul 2010
Posts: 5,765
Re: C++ Threading w/ Boost Library

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) << " " );
}
leavesofliberty is offline   Reply With Quote
Old 03-05-2017, 06:30 PM   #7
RustyBrooks
Carpal \'Tunnel
 
RustyBrooks's Avatar
 
Join Date: Feb 2006
Location: Austin, TX
Posts: 23,124
Re: C++ Threading w/ Boost Library

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.
RustyBrooks is offline   Reply With Quote
Old 03-05-2017, 06:41 PM   #8
leavesofliberty
Pooh-Bah
 
Join Date: Jul 2010
Posts: 5,765
Re: C++ Threading w/ Boost Library

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.
leavesofliberty is offline   Reply With Quote
Old 03-05-2017, 06:47 PM   #9
RustyBrooks
Carpal \'Tunnel
 
RustyBrooks's Avatar
 
Join Date: Feb 2006
Location: Austin, TX
Posts: 23,124
Re: C++ Threading w/ Boost Library

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.
RustyBrooks is offline   Reply With Quote
Old 03-05-2017, 06:53 PM   #10
leavesofliberty
Pooh-Bah
 
Join Date: Jul 2010
Posts: 5,765
Re: C++ Threading w/ Boost Library

Quote:
Originally Posted by RustyBrooks View Post
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.
leavesofliberty is offline   Reply With Quote
Old 03-05-2017, 07:45 PM   #11
leavesofliberty
Pooh-Bah
 
Join Date: Jul 2010
Posts: 5,765
Re: C++ Threading w/ Boost Library

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.
leavesofliberty is offline   Reply With Quote
Old 03-05-2017, 07:56 PM   #12
jukofyork
Carpal \'Tunnel
 
jukofyork's Avatar
 
Join Date: Sep 2004
Location: Glastonbury Grove
Posts: 11,247
Re: C++ Threading w/ Boost Library

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
jukofyork is offline   Reply With Quote
Old 03-05-2017, 08:02 PM   #13
leavesofliberty
Pooh-Bah
 
Join Date: Jul 2010
Posts: 5,765
Re: C++ Threading w/ Boost Library

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
leavesofliberty is offline   Reply With Quote
Old 03-05-2017, 08:04 PM   #14
jukofyork
Carpal \'Tunnel
 
jukofyork's Avatar
 
Join Date: Sep 2004
Location: Glastonbury Grove
Posts: 11,247
Re: C++ Threading w/ Boost Library

Try "-lboost_thread" instead of "-l boost_thread".

Juk
jukofyork is offline   Reply With Quote
Old 03-05-2017, 08:05 PM   #15
jukofyork
Carpal \'Tunnel
 
jukofyork's Avatar
 
Join Date: Sep 2004
Location: Glastonbury Grove
Posts: 11,247
Re: C++ Threading w/ Boost Library

Also try adding "lboost_chrono" and see if that helps.

Juk
jukofyork is offline   Reply With Quote
Old 03-05-2017, 08:10 PM   #16
leavesofliberty
Pooh-Bah
 
Join Date: Jul 2010
Posts: 5,765
Re: C++ Threading w/ Boost Library

Quote:
Originally Posted by jukofyork View Post
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.
leavesofliberty is offline   Reply With Quote
Old 03-05-2017, 08:16 PM   #17
jukofyork
Carpal \'Tunnel
 
jukofyork's Avatar
 
Join Date: Sep 2004
Location: Glastonbury Grove
Posts: 11,247
Re: C++ Threading w/ Boost Library

Try:
Code:
g++ -std=c++11 thread.cpp -lpthread -lboost_system -lboost_chrono -lboost_thread -o thread.o
Juk
jukofyork is offline   Reply With Quote
Old 03-05-2017, 08:21 PM   #18
RustyBrooks
Carpal \'Tunnel
 
RustyBrooks's Avatar
 
Join Date: Feb 2006
Location: Austin, TX
Posts: 23,124
Re: C++ Threading w/ Boost Library

Yeah, annoyingly, the order of the libraries matters.

And I actually agree that you should probably just use c++11 threads.
RustyBrooks is offline   Reply With Quote
Old 03-05-2017, 08:24 PM   #19
leavesofliberty
Pooh-Bah
 
Join Date: Jul 2010
Posts: 5,765
Re: C++ Threading w/ Boost Library

Quote:
Originally Posted by jukofyork View Post
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:
leavesofliberty is offline   Reply With Quote
Old 03-05-2017, 08:28 PM   #20
jukofyork
Carpal \'Tunnel
 
jukofyork's Avatar
 
Join Date: Sep 2004
Location: Glastonbury Grove
Posts: 11,247
Re: C++ Threading w/ Boost Library

Quote:
Originally Posted by leavesofliberty View Post
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
jukofyork is offline   Reply With Quote
Old 03-05-2017, 08:37 PM   #21
jukofyork
Carpal \'Tunnel
 
jukofyork's Avatar
 
Join Date: Sep 2004
Location: Glastonbury Grove
Posts: 11,247
Re: C++ Threading w/ Boost Library

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
jukofyork is offline   Reply With Quote
Old 03-05-2017, 08:47 PM   #22
leavesofliberty
Pooh-Bah
 
Join Date: Jul 2010
Posts: 5,765
Re: C++ Threading w/ Boost Library

Quote:
Originally Posted by jukofyork View Post
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:
leavesofliberty is offline   Reply With Quote
Old 03-05-2017, 10:29 PM   #23
leavesofliberty
Pooh-Bah
 
Join Date: Jul 2010
Posts: 5,765
Re: C++ Threading w/ Boost Library

Quote:
Originally Posted by jukofyork View Post
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.
leavesofliberty is offline   Reply With Quote
Old 03-06-2017, 01:51 AM   #24
leavesofliberty
Pooh-Bah
 
Join Date: Jul 2010
Posts: 5,765
Re: C++ Threading w/ Boost Library

I decided to uninstall Boost, and re-install it, and now it works!

leavesofliberty is offline   Reply With Quote
Old 03-06-2017, 04:50 AM   #25
goofyballer
 
goofyballer's Avatar
 
Join Date: Jun 2005
Posts: 59,982
Re: C++ Threading w/ Boost Library

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.
goofyballer is offline   Reply With Quote

Reply
      

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Forum Jump


All times are GMT -4. The time now is 06:20 AM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © 2008-2010, Two Plus Two Interactive
 
 
Poker Players - Streaming Live Online