gpt4 book ai didi

不调用 Thread.join() 时的 C++ 多线程异常

转载 作者:行者123 更新时间:2023-11-30 05:26:01 26 4
gpt4 key购买 nike

我有一个非常简单的程序,它给出了一个我不理解的异常。我有一个用 Visual Studio Express 2012 for Desktop 编写的 C++ 应用程序。

我创建了一个多线程应用程序,其中有 2 个线程,一个调用一个 person 类,另一个调用另一个 car 类。

当我使用 thread.join() 时,我没有得到异常,但我没有得到所需的输出。当我注释掉 thread.join() 时,我得到了异常,但我也得到了所需的输出。

异常(exception)情况是: enter image description here

我只是想让两个线程同时执行和处理。 person 和 car 类都调用了一个方法,该方法使用线程 sleep 重复 10 次,它所做的只是为 person 类打印一个字符串“started walking”,为 car 类打印一个字符串“started driving”,主线程只有“主要等待...”

所以程序的输出是:

started walking
started driving
started walking
started driving
main waiting...
started walking
started driving
started walking
started driving
main waiting...

等这就是我想要的。

如果我取消注释 join 方法,我会得到这个输出:

started walking
started walking
(repeat 8 more times)
started driving
started driving
(repeat 8 more times)
Main waiting..
Main waiting..
(repeat 8 more times)

这不是我想要的。

如何在不使用 join() 方法的情况下让这个应用程序在没有异常的情况下执行?

仅供引用,下面是我创建的示例代码:

这是启动程序的主要方法。

include <iostream>
#include <thread>
#include <chrono>
#include "person.h"
#include "car.h"

int count1 = 0;
int count2 = 0;

int main()
{
person p1;
thread t1(&person::startWalking, p1);
if(t1.joinable())
{
//t1.join();
}

car c1;
thread t2(&car::startDriving, c1);
if(t2.joinable())
{
//t2.join();
}

for(int x = 0; x < 10; x++)
{
std::cout<<"Main waiting...\n";//<<std::endl;
}
std::this_thread::sleep_for(std::chrono::milliseconds(5000));
return 0;
}

这是 person.cpp 文件:

#include "person.h"
person::person(void)
{
}
person::~person(void)
{
}
void person::startWalking()
{
for(int x = 0; x < 10; x++)
{
cout<<"Started Walking.\n";
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
}

void person::stopWalking()
{
cout<<"Stopped Walking.\n";
}

这是 car.cpp 文件:

#include "car.h"

using namespace std;

car::car(void)
{
}
car::~car(void)
{
}
void car::startDriving()
{
for(int x = 0; x< 10; x++)
{
std::cout<<"started driving\n.";
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
}

最佳答案

首先,document读到 如果线程在销毁时是可连接的,则调用 terminate()。,因此如果您注释掉 thread.join() 并且 main 函数已完成,则线程必须调用析构函数,它会调用terminate,而terminate在这里是abort,所以你得到了一个异常!

如果取消注释 thread.join(),join方法 在线程执行完成时返回。 因此您无法获得异常(exception)的答案。

建议:

移动

if(t1.joinable())
{
t1.join();
}

if(t2.joinable())
{
t2.join();
}

std::this_thread::sleep_for(std::chrono::milliseconds(5000)); 之前。你可以看到这三个线程同时执行,但是你也得不到你想要的结果,因为t1t2休眠了1秒,所以主线程会打印Main waiting... 非常快。所以你应该注释掉t1t2中的sleep或者在for的每个循环中添加相同的休眠时间主要。

提示:这三个线程会竞争,输出缓冲区会随机填充,因此输出将不可预测,您可以添加互斥锁以确保输出可读。

这是我的引用代码:

#include <iostream>
#include <thread>
#include <chrono>
#include <mutex>

std::mutex mMutex;

class person {
public:
void startWalking()
{
for(int x = 0; x < 10; x++)
{
mMutex.lock();
std::cout<< "Started Walking.\n";
mMutex.unlock();
//std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
}
};

class car {
public:
void startDriving()
{
for(int x = 0; x< 10; x++)
{
mMutex.lock();
std::cout <<"Started driving.\n";
mMutex.unlock();
//std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
}
};

int main()
{
person p1;
std::thread t1(&person::startWalking, p1);

car c1;
std::thread t2(&car::startDriving, c1);

for(int x = 0; x < 10; x++)
{
mMutex.lock();
std::cout <<"Main waiting...\n";
mMutex.unlock();
//std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
t1.join();
t2.join();
std::this_thread::sleep_for(std::chrono::milliseconds(5000));
return 0;
}

关于不调用 Thread.join() 时的 C++ 多线程异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38004563/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com