Skip to content
Search

Boost:Thread

lastmod 2018-08-17created 2015-03-187 edits

Portable C++ multi-threading.

How to use

To use Boost.Thread, configure the following.

  1. Define BOOST_THREAD_USE_LIB.
  2. Link libboost_thread.a.
  3. Link libboost_system.a if needed.

Example

#include <iostream>
#include <boost/thread.hpp>
using namespace std;

class CSampleIO
{
public:
    void TestThread()
    {
        while (1) {
            cout << "1 ";
            boost::this_thread::sleep(boost::posix_time::millisec(500));
        }
    }

    void TestThreadSecond(int num)
    {
        while (1) {
            cout<<num<<" ";
            boost::this_thread::sleep(boost::posix_time::millisec(500));
        }
    }

    void TestThreadThird(int num, int num2)
    {
        while (1) {
            cout<<num<<" ";
            boost::this_thread::sleep(boost::posix_time::millisec(500));
        }
    }
};

int main()
{
    CSampleIO io;

    // 스레드생성 (인자는 계속 추가 시킬 수 있음).
    boost::thread th1 = boost::thread( boost::bind(&CSampleIO::TestThread, &io)  );
    boost::thread th2 = boost::thread( boost::bind(&CSampleIO::TestThreadSecond, &io, 2)  );
    boost::thread th3 = boost::thread( boost::bind(&CSampleIO::TestThreadThird, &io, 3, NULL)  );

    // join에서 해당 스레드가 시작 됨.
    th1.join();
    th2.join();

    return 0;
}

For a detailed description of Boost::Thread, see std::thread instead.

Troubleshooting

undefined reference to boost::detail::set_tss_data

The following error may occur.

./libworld-g.a(Log.world-g.o): In function `ZN5boost19thread_specific_ptrINS_3log9v2_mt_nt55sinks30basic_formatting_sink_frontendIcE18formatting_contextEED1Ev':
C:/workspace/root/var/opm/local/include/boost/thread/tss.hpp:79: undefined reference to `boost::detail::set_tss_data(void const*, boost::shared_ptr<boost::detail::tss_cleanup_function>, void*, bool)'
./libworld-g.a(Log.world-g.o): In function `ZNK5boost19thread_specific_ptrINS_3log9v2_mt_nt55sinks30basic_formatting_sink_frontendIcE18formatting_contextEE3getEv':
C:/workspace/root/var/opm/local/include/boost/thread/tss.hpp:84: undefined reference to `boost::detail::get_tss_data(void const*)'
./libworld-g.a(Log.world-g.o): In function `ZN5boost19thread_specific_ptrINS_3log9v2_mt_nt55sinks30basic_formatting_sink_frontendIcE18formatting_contextEE5resetEPS6_':
C:/workspace/root/var/opm/local/include/boost/thread/tss.hpp:105: undefined reference to `boost::detail::set_tss_data(void const*, boost::shared_ptr<boost::detail::tss_cleanup_function>, void*, bool)'

In this case, link libboost_thread. Even if you use C++11's std::thread, you must add it to the linker when using Boost:Log, etc.

See also

Favorite site

References


  1. Unavailable_the_boost_threads_library.pdf ↩