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
Multithreading 为什么在构造函数中创建线程会导致运行时错误 我在C++中有下面的代码,我尝试从类的构造函数中生成新的线程。我得到运行时错误。我不明白这是为什么,以及如何预防。我是C++的新手。在VS2017中,调用了abort()中的it错误 #include <iostream> #include <thread> #include <chrono> class Rectangle { public: int area(void); Rectangle(); ~Rectangle(); private: int x, y; int UpdateData(); }; Rectangle::Rectangle() { x = 1; y = 2; std::thread modelThread(&Rectangle::UpdateData, this); } Rectangle::~Rectangle() { } int Rectangle::area() { return x * y; } int Rectangle::UpdateData() { while (true) { x++; y += 2; std::cout << "Area with x:" << x << " y:" << y << " area:" << Rectangle::area() << std::endl; std::this_thread::sleep_for(std::chrono::milliseconds(2000)); } return 0; } int main() { Rectangle rect; return 0; } #包括 #包括 #包括 类矩形{ 公众: 内部区域(空白); 矩形(); ~Rectangle(); 私人: int x,y; int UpdateData(); }; 矩形::矩形(){ x=1; y=2; std::threadmodelthread(&Rectangle::UpdateData,this); } 矩形::~Rectangle(){ } int矩形::区域(){ 返回x*y; } int Rectangle::UpdateData() { while(true){ x++; y+=2; std::cout_Multithreading_C++11 - Fatal编程技术网

Multithreading 为什么在构造函数中创建线程会导致运行时错误 我在C++中有下面的代码,我尝试从类的构造函数中生成新的线程。我得到运行时错误。我不明白这是为什么,以及如何预防。我是C++的新手。在VS2017中,调用了abort()中的it错误 #include <iostream> #include <thread> #include <chrono> class Rectangle { public: int area(void); Rectangle(); ~Rectangle(); private: int x, y; int UpdateData(); }; Rectangle::Rectangle() { x = 1; y = 2; std::thread modelThread(&Rectangle::UpdateData, this); } Rectangle::~Rectangle() { } int Rectangle::area() { return x * y; } int Rectangle::UpdateData() { while (true) { x++; y += 2; std::cout << "Area with x:" << x << " y:" << y << " area:" << Rectangle::area() << std::endl; std::this_thread::sleep_for(std::chrono::milliseconds(2000)); } return 0; } int main() { Rectangle rect; return 0; } #包括 #包括 #包括 类矩形{ 公众: 内部区域(空白); 矩形(); ~Rectangle(); 私人: int x,y; int UpdateData(); }; 矩形::矩形(){ x=1; y=2; std::threadmodelthread(&Rectangle::UpdateData,this); } 矩形::~Rectangle(){ } int矩形::区域(){ 返回x*y; } int Rectangle::UpdateData() { while(true){ x++; y+=2; std::cout

Multithreading 为什么在构造函数中创建线程会导致运行时错误 我在C++中有下面的代码,我尝试从类的构造函数中生成新的线程。我得到运行时错误。我不明白这是为什么,以及如何预防。我是C++的新手。在VS2017中,调用了abort()中的it错误 #include <iostream> #include <thread> #include <chrono> class Rectangle { public: int area(void); Rectangle(); ~Rectangle(); private: int x, y; int UpdateData(); }; Rectangle::Rectangle() { x = 1; y = 2; std::thread modelThread(&Rectangle::UpdateData, this); } Rectangle::~Rectangle() { } int Rectangle::area() { return x * y; } int Rectangle::UpdateData() { while (true) { x++; y += 2; std::cout << "Area with x:" << x << " y:" << y << " area:" << Rectangle::area() << std::endl; std::this_thread::sleep_for(std::chrono::milliseconds(2000)); } return 0; } int main() { Rectangle rect; return 0; } #包括 #包括 #包括 类矩形{ 公众: 内部区域(空白); 矩形(); ~Rectangle(); 私人: int x,y; int UpdateData(); }; 矩形::矩形(){ x=1; y=2; std::threadmodelthread(&Rectangle::UpdateData,this); } 矩形::~Rectangle(){ } int矩形::区域(){ 返回x*y; } int Rectangle::UpdateData() { while(true){ x++; y+=2; std::cout,multithreading,c++11,Multithreading,C++11,main和modelThread是线程,但是modelThread通过Rectangle的构造函数从main启动。但是,当main结束时,rect超出范围并被销毁,而在其构造函数中启动的线程仍在运行。这会导致运行时间延迟e错误。 因此,您必须将modelThread加入main线程,以确保main等待modelThread完成运行 但现在,我无法执行main函数中的代码。如何实现这两个功能 为此,您必须将UpdateDatapublic,并在main中启动modelThread,如下所示: st

main
modelThread
是线程,但是
modelThread
通过
Rectangle
的构造函数从
main
启动。但是,当main结束时,
rect
超出范围并被销毁,而在其构造函数中启动的线程仍在运行。这会导致运行时间延迟e错误。 因此,您必须将
modelThread
加入
main
线程,以确保main等待
modelThread
完成运行

但现在,我无法执行main函数中的代码。如何实现这两个功能

为此,您必须将
UpdateData
public
,并在main中启动
modelThread
,如下所示:

std::thread modelThread(&Rectangle::UpdateData, &rect);
std::future<int> fut =  std::async (&Rectangle::UpdateData, &rect);
int Rectangle::UpdateData()
{
    std::lock_guard<std::mutex> lock(mtx);
    while (true) {
        x++;
        y += 2;
        std::cout << "Area with x:" << x << " y:" << y << " area:" << Rectangle::area() << std::endl;
        std::this_thread::sleep_for(std::chrono::milliseconds(2000));
    }
    return 0;
}
即使使用
std::async
,您也必须将
UpdateData
设置为public
,并像这样使用它:

std::thread modelThread(&Rectangle::UpdateData, &rect);
std::future<int> fut =  std::async (&Rectangle::UpdateData, &rect);
int Rectangle::UpdateData()
{
    std::lock_guard<std::mutex> lock(mtx);
    while (true) {
        x++;
        y += 2;
        std::cout << "Area with x:" << x << " y:" << y << " area:" << Rectangle::area() << std::endl;
        std::this_thread::sleep_for(std::chrono::milliseconds(2000));
    }
    return 0;
}
然后,您可以在
main
中执行代码,而无需等待
UpdateData
完成。然后,您可以使用
main
中的以下命令获取
UpdateData
函数计算的值

int ret = fut.get();      
如果希望
main
UpdateData
的输出不会混淆,请执行此操作。
矩形中声明
private
成员变量

std::mutex mtx; //uses <mutex>
std::mutex mtx;//使用
然后像这样使用它:

std::thread modelThread(&Rectangle::UpdateData, &rect);
std::future<int> fut =  std::async (&Rectangle::UpdateData, &rect);
int Rectangle::UpdateData()
{
    std::lock_guard<std::mutex> lock(mtx);
    while (true) {
        x++;
        y += 2;
        std::cout << "Area with x:" << x << " y:" << y << " area:" << Rectangle::area() << std::endl;
        std::this_thread::sleep_for(std::chrono::milliseconds(2000));
    }
    return 0;
}
intrectangle::UpdateData()
{
标准:锁和防护锁(mtx);
while(true){
x++;
y+=2;

std::cout
main
modelThread
是线程,但是
modelThread
通过
Rectangle
的构造函数从
main
启动。但是,当main结束时,
rect
超出范围并被销毁,而在其构造函数中启动的线程仍在运行。这会导致运行时间延迟e错误。 因此,您必须将
modelThread
加入
main
线程,以确保main等待
modelThread
完成运行

但现在,我无法执行main函数中的代码。如何实现这两个功能

为此,您必须将
UpdateData
public
,并在main中启动
modelThread
,如下所示:

std::thread modelThread(&Rectangle::UpdateData, &rect);
std::future<int> fut =  std::async (&Rectangle::UpdateData, &rect);
int Rectangle::UpdateData()
{
    std::lock_guard<std::mutex> lock(mtx);
    while (true) {
        x++;
        y += 2;
        std::cout << "Area with x:" << x << " y:" << y << " area:" << Rectangle::area() << std::endl;
        std::this_thread::sleep_for(std::chrono::milliseconds(2000));
    }
    return 0;
}
即使使用
std::async
,您也必须将
UpdateData
设置为public
,并像这样使用它:

std::thread modelThread(&Rectangle::UpdateData, &rect);
std::future<int> fut =  std::async (&Rectangle::UpdateData, &rect);
int Rectangle::UpdateData()
{
    std::lock_guard<std::mutex> lock(mtx);
    while (true) {
        x++;
        y += 2;
        std::cout << "Area with x:" << x << " y:" << y << " area:" << Rectangle::area() << std::endl;
        std::this_thread::sleep_for(std::chrono::milliseconds(2000));
    }
    return 0;
}
然后,您可以在
main
中执行代码,而无需等待
UpdateData
完成。然后,您可以使用
main
中的以下命令获取
UpdateData
函数计算的值

int ret = fut.get();      
如果希望
main
UpdateData
的输出不会混淆,请执行此操作。
矩形中声明
private
成员变量

std::mutex mtx; //uses <mutex>
std::mutex mtx;//使用
然后像这样使用它:

std::thread modelThread(&Rectangle::UpdateData, &rect);
std::future<int> fut =  std::async (&Rectangle::UpdateData, &rect);
int Rectangle::UpdateData()
{
    std::lock_guard<std::mutex> lock(mtx);
    while (true) {
        x++;
        y += 2;
        std::cout << "Area with x:" << x << " y:" << y << " area:" << Rectangle::area() << std::endl;
        std::this_thread::sleep_for(std::chrono::milliseconds(2000));
    }
    return 0;
}
intrectangle::UpdateData()
{
标准:锁和防护锁(mtx);
while(true){
x++;
y+=2;

std::cout
modelThread
对象在线程仍在运行时被销毁..您需要确保
thread
对象比线程寿命长(通常通过对其调用
join()
),或者
detach()
线程。谢谢。我理解了它产生错误的原因。我添加了modelThread.join()。运行时错误现在消失了。但现在,我无法执行main函数中的代码。如何实现这两个功能。在示例中使用线程没有意义,因为在等待过程中没有其他事情要做。并且没有退出条件,因此程序将永远运行,最终计算区域将溢出(然后是y,然后是x)此外,由于代码中没有同步,所以无论如何都不能安全地使用其他线程的数据。首先,通过读取C++中并发的好的操作来更好地学习多线程,一旦您对其工作方式有了一点了解,就尝试编写一些代码。ode>modelThread
Rectangle
构造函数中,您不能从main等待它。因此线程对象应该在
main
中声明……但是如果没有适当的存在条件,您无论如何都会无限等待。在您的情况下,可能更容易使用
std::async
modelThread
对象是线程仍在运行时销毁。您需要确保
thread
对象比线程更有效(通常通过对其调用
join()
),或者
detach()
线程。谢谢。我理解了它产生错误的原因。我添加了modelThread.join()。运行时错误现在消失了。但现在,我无法执行main函数中的代码。如何实现这两个功能。在示例中使用线程没有意义,因为在等待过程中没有其他事情要做。并且没有退出条件,因此程序将永远运行,最终计算区域将溢出(然后是y,然后是x)此外,由于代码中没有同步,所以无论如何都无法安全地使用其他线程的数据。首先,通过读取C++中的并发操作以及一旦有最小值,就可以学习多线程。