1

I would prefer not to download anything, but if I must, I can do so. I am just trying to run a simple multi-threaded program using the Boost library on many of the online compilers, but none of them even recognize

#include <boost/thread.hpp>

and

using namespace boost::this_thread;

The code itself is taken from this link: https://www.quantnet.com/threads/c-multithreading-in-boost.10028/

I have done my googling and tried out a lot of online compilers but none of them seem willing to recognize Boost or its associated libraries.

This is the code:

#include <iostream>
#include <boost/thread.hpp>

using namespace std;
using namespace boost;
using namespace boost::this_thread;

// Global function called by thread
void GlobalFunction()
{
   for (int i=0; i<10; ++i) {
       cout<< i << "Do something in parallel with main method." << endl;
       boost::this_thread::yield(); // 'yield' discussed in section 18.6
   }
}

int main()
{
    boost::thread t(&GlobalFunction);
    for (int i=0; i<10; i++) {
        cout << i <<"Do something in main method."<<endl;
    }
    return 0;
}
mvp
  • 111,019
  • 13
  • 122
  • 148
user2881037
  • 23
  • 1
  • 3

3 Answers3

4

Wandbox is an online C++ IDE that contains BOOST libraries. It supports up-to-date BOOST version (currently 1.67.0)

Note: your code example works fine in BOOST versions up to 1.64.0.

Daniel Heilper
  • 1,182
  • 2
  • 17
  • 34
1

just use the C++11 threads. Ideone has threading disabled apparently but I had no problem running it on http://www.compileonline.com/ (just be sure to select C++11 and not C++)

#include <iostream>
#include <thread>
// Global function called by thread
void GlobalFunction()
{
    for (int i = 0; i < 10; ++i)
    {
        std::cout << i << "Do something in parallel with main method." << std::endl;
        std::this_thread::yield(); // 'yield' discussed in section 18.6
    }
}


int main()
{
    std::thread t(&GlobalFunction);

    for (int i = 0; i < 10; i++)
    {
        std::cout << i << "Do something in main method." << std::endl;
    }


    return 0;
}
PeterT
  • 7,981
  • 1
  • 26
  • 34
  • Is "thread" one of the boost libraries added to C++11, which is why I don't see the word "boost" anywhere in the program? – user2881037 Oct 15 '13 at 03:47
  • @user2881037 it's in the standard now, yes. A few things [have changed](http://stackoverflow.com/questions/7241993/is-it-smart-to-replace-boostthread-and-boostmutex-with-c11-equivalents) from the boost implementation though. As is often the case. – PeterT Oct 15 '13 at 03:50
  • PeterT - Even though I didn't need it in this case, do you know if online compilers (like the one you mentioned) support Boost libraries, i.e. #include or the using namespace boost ? Thanks. – user2881037 Oct 15 '13 at 03:59
  • @user2881037 I don't use them too often, so I don't know. But the link you posted suggests so, but probably not every part of Boost (Boost is a collection of libraries and not just a singular library). Oh, and just to be pedantic every one of the compilers will probably compile `namespace boost{}; using namespace boost;` there's nothing special about the namespace boost. Whether you declare it yourself or include a library that declares it. – PeterT Oct 15 '13 at 04:02
  • PeterT - Perhaps they only support the Boost libraries that have been included in the C++11 standard. – user2881037 Oct 15 '13 at 04:21
0

if you need to use boost, you have to download it ... those headers are not present on you computer.

Zac Wrangler
  • 1,445
  • 9
  • 8
  • There is another user on here who managed to get this to work in the following link : http://ideone.com/Vuueb. I tried this same short example, and it gave me a 'No file or directory' error. – user2881037 Oct 15 '13 at 03:32
  • 1
    ideone installs a boost version at their server. – Zac Wrangler Oct 15 '13 at 03:36