C++ C+中的线程向量+;

C++ C+中的线程向量+;,c++,multithreading,vector,C++,Multithreading,Vector,我试图使用线程向量。我将收到一个int作为参数,它是我必须创建的线程数 我试过这样的方法: #include <iostream> #include <thread> #include <vector> void credo() { std::cout << "threads" << std::endl; } void threads_creation() { int i; std::vector<st

我试图使用线程向量。我将收到一个int作为参数,它是我必须创建的线程数

我试过这样的方法:

#include <iostream>
#include <thread>
#include <vector>

void credo()
{
    std::cout << "threads" << std::endl;
}

void threads_creation()
{
    int i;
    std::vector<std::thread> threads_tab;
    i = 0;
    while(i < 2)
    {
        threads_tab.push_back(std::thread(credo));
        i++;
    }
}

int main()
{
    threads_creation();
    return (0);
}

这里怎么了?

std::thread类在Linux中使用pthreads,因此您需要在命令中添加
-pthread
编译器标志

g++ -W -Wall -Werror -Wextra -pthread threads.cpp

std::thread类在Linux中使用pthreads,因此需要在命令中添加
-pthread
编译器标志

g++ -W -Wall -Werror -Wextra -pthread threads.cpp

为什么在返回类型和函数名之间有一堵中国墙?我已经有一段时间没有使用pthread了,但是,您是否安装了pthread库?或正确引用?如果是这样,可能需要指定一些选项来指示.o对象的位置are@DeiDei我的学校要求我这样做,作为“标准”,这不是问题,但是你真的需要
std::endl
所需要的额外的东西吗<代码>'\n'结束一行。为什么在返回类型和函数名之间有一堵中国墙?我已经有一段时间没有使用pthread了,但是,您是否安装了pthread库?或正确引用?如果是这样,可能需要指定一些选项来指示.o对象的位置are@DeiDei我的学校要求我这样做,作为“标准”,这不是问题,但是你真的需要
std::endl
所需要的额外的东西吗<代码>“\n”结束一行。
g++ -W -Wall -Werror -Wextra -pthread threads.cpp