Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/142.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在类中创建线程并在c+中的类中的函数上执行它+;(cpp)_C++_Multithreading - Fatal编程技术网

C++ 在类中创建线程并在c+中的类中的函数上执行它+;(cpp)

C++ 在类中创建线程并在c+中的类中的函数上执行它+;(cpp),c++,multithreading,C++,Multithreading,我想制作一个有多个线程的程序,线程之间的唯一交互应该是为线程之间的传输而创建的几个变量。其中之一是队列。我希望有一个线程不断地写入文件,我希望其他线程不断地给它行来写入 但出于简单的原因,我希望通过创建类,然后从该类中调用名为give_line()的函数来实现这一切。我希望那个正在写入文件的线程是私有的,这样其他人就不会被它搞乱了 class bar { private: std::queue<std::string> lines; // The queue v

我想制作一个有多个线程的程序,线程之间的唯一交互应该是为线程之间的传输而创建的几个变量。其中之一是队列。我希望有一个线程不断地写入文件,我希望其他线程不断地给它行来写入

但出于简单的原因,我希望通过创建类,然后从该类中调用名为give_line()的函数来实现这一切。我希望那个正在写入文件的线程是私有的,这样其他人就不会被它搞乱了

class bar
{
private:
    std::queue<std::string> lines;    // The queue
    void write()                      // The function we call from thread
    {
        std::cout << "Hello world from other thread";
    }
public:
    bar()                             // constructor
    {
        std::thread writer(&bar::write, this);     // thread
    }
}

int main()
{
    bar testing;
    std::cout << "Hello World from main thread" << std::endl;

    /*
    What this does is it allows me to keep the console open till we get a enter key.
    */

    std::cin.clear();
    std::cin.ignore(32767, '\n');
    std::cin.get();


    return 0;
}
为什么这毫无意义,为什么它不起作用,我都不明白。它启动了,然后砰的一声,它崩溃了。我想不出来。当我在一个类中没有它时,它就可以工作了,但是其他人可能会弄乱线程,我真的不希望这样。除非我需要,否则我不会从项目中发布太多代码。我创建这个类以及其他函数和变量的原因是您不需要知道的


我知道我需要一个作家。join()在什么地方。但那是以后的事了。我想让另一个变量能够立即结束一切。该变量只能从主线程访问,因此其他线程不能处理它。有点像杀戮变量

这里有多个问题,但首先也是最重要的是,
std::thread
的构造函数在新启动的线程中执行一个函数。看起来您不是在传递函数,而是在传递类方法

我相当肯定,当你的编译器试图编译这段代码时,它是在对你大喊大叫。尽管报告了错误,但编译器选择生成某种已编译的对象文件并不意味着可以忽略编译器诊断

你可能想做这样的事情:

static void invoke_write(bar *p)
{
     p->write();
}

bar()
{
     std::thread writer(&invoke_write, this); 
}
bar()
{
     std::thread writer(&invoke_write, this); 

     writer.join();
}
现在,您正在调用一个静态函数,并向它传递一个指向实例化类的指针,该类用于调用类方法

但崩溃的真正原因是,您正在
bar
的构造函数范围内实例化
std::thread
的实例

bar
的构造函数返回时,
std::thread
对象就像任何其他函数/方法作用域的对象一样,被销毁,并调用其析构函数。但是,您刚刚启动了正在运行的线程,如果在线程仍在运行时调用了
std::thread
的析构函数,就会调用
terminate()
,中止程序

如果线程正在运行,则必须在销毁
std::thread
的实例之前
join()
。所以,要么像这样:

static void invoke_write(bar *p)
{
     p->write();
}

bar()
{
     std::thread writer(&invoke_write, this); 
}
bar()
{
     std::thread writer(&invoke_write, this); 

     writer.join();
}
或者,将
std::thread
的实例作为类成员,在构造函数中实例化它,并在析构函数中加入它。比如:

    bar() : writer(&invoke_write, this)
    {
    }

    ~bar()
    {
         writer.join();
    }

private:

    std::thread writer;

当然,哪种方法是正确的取决于应用程序的需求。

对类的一个简单更改是,在创建线程对象后,确保不会立即丢失线程对象

class bar
{
private:
    std::queue<std::string> lines;    // The queue
    std::thread writer;

    void write()                      // The function we call from thread
    {
        std::cout << "Hello world from other thread";
    }
public:
    bar() : writer{&bar::write, this} {}

    ~bar()
    {
        writer.join();
    }
};
类栏
{
私人:
std::queue lines;//队列
线程编写器;
void write()//我们从线程调用的函数
{

啊,是的,你是对的。它确实会杀死你的程序。