我正在尝试运行使用Boost库的C ++代码

时间:2013-10-15 03:27:49

标签: c++ boost compiler-construction

我不想下载任何东西,但如果必须,我可以这样做。我只是试图在许多在线编译器上使用Boost库运行一个简单的多线程程序,但它们都没有识别

#include <boost/thread.hpp>

using namespace boost::this_thread;

代码本身取自此链接: https://www.quantnet.com/threads/c-multithreading-in-boost.10028/

我已经完成了我的谷歌搜索并尝试了很多在线编译器,但他们似乎都不愿意识别Boost或其相关的库。

这是代码:

#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;
}

3 个答案:

答案 0 :(得分:1)

只使用C ++ 11线程。 Ideone显然禁用了线程,但我在http://www.compileonline.com/上运行它没有问题(只需确保选择C ++ 11而不是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;
}

答案 1 :(得分:0)

如果你需要使用boost,你必须下载它......你的计算机上没有这些标题。

答案 2 :(得分:0)

Wandbox是一个包含BOOST库的在线C ++ IDE。它支持最新的BOOST版本(目前为1.67.0)

注意:您的代码示例在最高1.64.0的BOOST版本中运行良好。

相关问题