我正在尝试使用升压库的C++代码

我正在尝试使用升压库的C++代码,c++,boost,compiler-construction,C++,Boost,Compiler Construction,我不想下载任何东西,但如果我必须下载,我可以这样做。我只是想在许多在线编译器上使用Boost库运行一个简单的多线程程序,但他们都不知道 #include <boost/thread.hpp> 代码本身取自此链接: 我在谷歌上搜索并试用了很多在线编译器,但似乎没有一个愿意识别Boost及其相关库 代码如下: #include <iostream> #include <boost/thread.hpp> using namespace std; using

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

#include <boost/thread.hpp>
代码本身取自此链接:

我在谷歌上搜索并试用了很多在线编译器,但似乎没有一个愿意识别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;
}

如果你需要使用boost,你必须下载它。。。这些头文件不在您的计算机上。

只需使用C++11线程即可。Ideone显然禁用了线程功能,但我在运行它时没有问题,只是确保选择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;
}

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


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

请在您的计算机上安装一个编译器。此处有另一个用户通过以下链接成功实现了此功能:。我尝试了同一个简短的示例,它给了我一个“无文件或目录”错误。ideone在他们的服务器上安装了一个boost版本。thread是添加到C++11的boost库之一吗,这就是为什么我在程序的任何地方都看不到boost这个词的原因?@user2881037它现在在标准中,是的。不过,boost实现中的一些东西。与通常的情况一样。PeterT-尽管在本例中我不需要它,但您知道像您提到的那样的在线编译器是否支持Boost库,即include或using namespace Boost?谢谢。@user2881037我不太经常使用它们,所以我不知道。但您发布的链接表明,Boost的每个部分可能都是一个库的集合,而不仅仅是一个单一的库。哦,只是为了学究,每个编译器可能都会编译名称空间boost{};使用名称空间boost;名称空间提升没有什么特别之处。无论您是自己声明它,还是包含声明它的库.PeterT—也许它们只支持C++11标准中包含的Boost库。
#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;
}