当一个工作线程失败时,如何中止剩余的工作线程

When one worker thread fails, how to abort remaining workers?

本文关键字:工作 线程 何中止 失败 一个      更新时间:2023-10-16

我有一个程序,它产生多个线程,每个线程执行一个长时间运行的任务。主线程等待所有工作线程加入,收集结果并退出。

如果其中一个工作线程发生错误,我希望剩余的工作线程优雅地停止,以便主线程可以很快退出。

我的问题是,当长时间运行任务的实现是由一个库提供的,而这个库的代码我无法修改时,如何最好地做到这一点。

下面是一个简单的系统草图,没有错误处理:

void threadFunc()
{
    // Do long-running stuff
}
void mainFunc()
{
    std::vector<std::thread> threads;
    for (int i = 0; i < 3; ++i) {
        threads.push_back(std::thread(&threadFunc));
    }
    for (auto &t : threads) {
        t.join();
    }
}

如果长时间运行的函数执行循环,并且我可以访问代码,则可以通过在每次迭代的顶部检查共享的"继续运行"标志来终止执行。

std::mutex mutex;
bool error;
void threadFunc()
{
    try {
        for (...) {
            {
                std::unique_lock<std::mutex> lock(mutex);
                if (error) {
                    break;
                }
            }
        }
    } catch (std::exception &) {
        std::unique_lock<std::mutex> lock(mutex);
        error = true;
    }
}

现在考虑库提供长时间运行操作的情况:

std::mutex mutex;
bool error;
class Task
{
public:
    // Blocks until completion, error, or stop() is called
    void run();
    void stop();
};
void threadFunc(Task &task)
{
    try {
        task.run();
    } catch (std::exception &) {
        std::unique_lock<std::mutex> lock(mutex);
        error = true;
    }
}
在这种情况下,主线程必须处理错误,并调用stop() on仍在运行的任务。因此,它不能简单地等待每个工人join()在原始实现中 到目前为止,我使用的方法是在以下结构之间共享主线程和每个工作线程:
struct SharedData
{
    std::mutex mutex;
    std::condition_variable condVar;
    bool error;
    int running;
}

当一个worker成功完成时,减少running计数。如果捕获异常时,工作线程设置error标志。在这两种情况下,它然后调用condVar.notify_one()

主线程等待条件变量,如果有,则唤醒设置errorrunning为零。在唤醒时,主线程如果设置了error,则在所有任务上调用stop()

这种方法是有效的,但我觉得应该有一个更干净的解决方案标准并发库中的高级原语。可以有人提出改进的实现吗?

下面是我当前解决方案的完整代码:

// main.cpp
#include <chrono>
#include <mutex>
#include <thread>
#include <vector>
#include "utils.h"
// Class which encapsulates long-running task, and provides a mechanism for aborting it
class Task
{
public:
    Task(int tidx, bool fail)
    :   tidx(tidx)
    ,   fail(fail)
    ,   m_run(true)
    {
    }
    void run()
    {
        static const int NUM_ITERATIONS = 10;
        for (int iter = 0; iter < NUM_ITERATIONS; ++iter) {
            {
                std::unique_lock<std::mutex> lock(m_mutex);
                if (!m_run) {
                    out() << "thread " << tidx << " aborting";
                    break;
                }
            }
            out() << "thread " << tidx << " iter " << iter;
            std::this_thread::sleep_for(std::chrono::milliseconds(100));
            if (fail) {
                throw std::exception();
            }
        }
    }
    void stop()
    {
        std::unique_lock<std::mutex> lock(m_mutex);
        m_run = false;
    }
    const int tidx;
    const bool fail;
private:
    std::mutex m_mutex;
    bool m_run;
};
// Data shared between all threads
struct SharedData
{
    std::mutex mutex;
    std::condition_variable condVar;
    bool error;
    int running;
    SharedData(int count)
    :   error(false)
    ,   running(count)
    {
    }
};
void threadFunc(Task &task, SharedData &shared)
{
    try {
        out() << "thread " << task.tidx << " starting";
        task.run(); // Blocks until task completes or is aborted by main thread
        out() << "thread " << task.tidx << " ended";
    } catch (std::exception &) {
        out() << "thread " << task.tidx << " failed";
        std::unique_lock<std::mutex> lock(shared.mutex);
        shared.error = true;
    }
    {
        std::unique_lock<std::mutex> lock(shared.mutex);
        --shared.running;
    }
    shared.condVar.notify_one();
}
int main(int argc, char **argv)
{
    static const int NUM_THREADS = 3;
    std::vector<std::unique_ptr<Task>> tasks(NUM_THREADS);
    std::vector<std::thread> threads(NUM_THREADS);
    SharedData shared(NUM_THREADS);
    for (int tidx = 0; tidx < NUM_THREADS; ++tidx) {
        const bool fail = (tidx == 1);
        tasks[tidx] = std::make_unique<Task>(tidx, fail);
        threads[tidx] = std::thread(&threadFunc, std::ref(*tasks[tidx]), std::ref(shared));
    }
    {
        std::unique_lock<std::mutex> lock(shared.mutex);
        // Wake up when either all tasks have completed, or any one has failed
        shared.condVar.wait(lock, [&shared](){
            return shared.error || !shared.running;
        });
        if (shared.error) {
            out() << "error occurred - terminating remaining tasks";
            for (auto &t : tasks) {
                t->stop();
            }
        }
    }
    for (int tidx = 0; tidx < NUM_THREADS; ++tidx) {
        out() << "waiting for thread " << tidx << " to join";
        threads[tidx].join();
        out() << "thread " << tidx << " joined";
    }
    out() << "program complete";
    return 0;
}

这里定义了一些实用函数:

// utils.h
#include <iostream>
#include <mutex>
#include <thread>
#ifndef UTILS_H
#define UTILS_H
#if __cplusplus <= 201103L
// Backport std::make_unique from C++14
#include <memory>
namespace std {
template<typename T, typename ...Args>
std::unique_ptr<T> make_unique(
            Args&& ...args)
{
    return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}
} // namespace std
#endif // __cplusplus <= 201103L
// Thread-safe wrapper around std::cout
class ThreadSafeStdOut
{
public:
    ThreadSafeStdOut()
    :   m_lock(m_mutex)
    {
    }
    ~ThreadSafeStdOut()
    {
        std::cout << std::endl;
    }
    template <typename T>
    ThreadSafeStdOut &operator<<(const T &obj)
    {
        std::cout << obj;
        return *this;
    }
private:
    static std::mutex m_mutex;
    std::unique_lock<std::mutex> m_lock;
};
std::mutex ThreadSafeStdOut::m_mutex;
// Convenience function for performing thread-safe output
ThreadSafeStdOut out()
{
    return ThreadSafeStdOut();
}
#endif // UTILS_H

我一直在考虑你的情况,这可能会对你有所帮助。你可以尝试几种不同的方法来实现你的目标。有2-3个选项可以使用或三者的组合。我将至少展示第一种选择,因为我仍在学习并试图掌握模板专门化的概念以及使用Lambdas。

  • 使用Manager类
  • 使用模板特化封装
  • 使用λ

Manager类的伪代码看起来像这样:

class ThreadManager {
private:
    std::unique_ptr<MainThread> mainThread_;
    std::list<std::shared_ptr<WorkerThread> lWorkers_;  // List to hold finished workers
    std::queue<std::shared_ptr<WorkerThread> qWorkers_; // Queue to hold inactive and waiting threads.
    std::map<unsigned, std::shared_ptr<WorkerThread> mThreadIds_; // Map to associate a WorkerThread with an ID value.
    std::map<unsigned, bool> mFinishedThreads_; // A map to keep track of finished and unfinished threads.
    bool threadError_; // Not needed if using exception handling
public:
    explicit ThreadManager( const MainThread& main_thread );
    void shutdownThread( const unsigned& threadId );
    void shutdownAllThreads();
    void addWorker( const WorkerThread& worker_thread );          
    bool isThreadDone( const unsigned& threadId );
    void spawnMainThread() const; // Method to start main thread's work.
    void spawnWorkerThread( unsigned threadId, bool& error );
    bool getThreadError( unsigned& threadID ); // Returns True If Thread Encountered An Error and passes the ID of that thread, 
};

为了简化结构,我使用bool值来确定线程是否失败,这只是为了演示的目的,当然,如果你喜欢使用异常或无效的无符号值等,这可以替换为你喜欢的。

现在使用这种类型的类应该是这样的:还要注意,这种类型的类如果是单例类型对象会被认为更好,因为您不需要超过1个ManagerClass,因为您正在使用共享指针。

SomeClass::SomeClass( ... ) {
    // This class could contain a private static smart pointer of this Manager Class
    // Initialize the smart pointer giving it new memory for the Manager Class and by passing it a pointer of the Main Thread object
   threadManager_ = new ThreadManager( main_thread ); // Wouldn't actually use raw pointers here unless if you had a need to, but just shown for simplicity       
}
SomeClass::addThreads( ... ) {
    for ( unsigned u = 1, u <= threadCount; u++ ) {
         threadManager_->addWorker( some_worker_thread );
    }
}
SomeClass::someFunctionThatSpawnsThreads( ... ) {
    threadManager_->spawnMainThread();
    bool error = false;       
    for ( unsigned u = 1; u <= threadCount; u++ ) {
        threadManager_->spawnWorkerThread( u, error );
        if ( error ) { // This Thread Failed To Start, Shutdown All Threads
            threadManager->shutdownAllThreads();
        }
    }
    // If all threads spawn successfully we can do a while loop here to listen if one fails.
    unsigned threadId;
    while ( threadManager_->getThreadError( threadId ) ) {
         // If the function passed to this while loop returns true and we end up here, it will pass the id value of the failed thread.
         // We can now go through a for loop and stop all active threads.
         for ( unsigned u = threadID + 1; u <= threadCount; u++ ) {
             threadManager_->shutdownThread( u );
         }
         // We have successfully shutdown all threads
         break;
    }
}

我喜欢管理器类的设计,因为我曾在其他项目中使用过它们,它们经常派上用场,特别是在处理包含许多资源的代码库时,例如具有许多资产(如精灵,纹理,音频文件,地图,游戏道具等)的工作游戏引擎。使用Manager类有助于跟踪和维护所有的资产。同样的概念可以应用于"管理"活动,非活动,等待线程,并知道如何直观地处理和关闭所有线程正确。如果您的代码库和库支持异常以及线程安全异常处理,我建议使用ExceptionHandler,而不是传递和使用错误处理工具。此外,拥有一个Logger类也很好,它可以在其中写入日志文件或控制台窗口,以提供显式消息,说明异常是在哪个函数中抛出的,以及是什么导致了异常,日志消息可能看起来像这样:

Exception Thrown: someFunctionNamedThis in ThisFile on Line# (x)
    threadID 021342 failed to execute.

通过这种方式,您可以查看日志文件并快速找出导致异常的线程,而不是使用传递的bool变量。

The implementation of the long-running task is provided by a library whose code I cannot modify.

这意味着你没有办法同步工作线程完成的任务

If an error occurs in one of the workers,

让我们假设你真的可以检测到工人错误;如果被使用的库报告,其中一些可以很容易地检测到,而另一些则不能,例如

  1. 库代码循环
  2. 库代码过早退出并出现未捕获的异常。

I want the remaining workers to stop **gracefully**

那是不可能的

你能做的最好的是写一个线程管理器检查工作线程的状态,如果检测到错误条件,它只是(不优雅地)"杀死"所有工作线程并退出。

您还应该考虑检测一个循环工作线程(通过超时),并为用户提供杀死或继续等待进程完成的选项。

你的问题是长时间运行的函数不是你的代码,你说你不能修改它。因此,你不能让它对任何类型的外部同步原语(条件变量、信号量、互斥锁、管道等)给予任何关注,除非库开发人员已经为你完成了这些。

因此,你唯一的选择是做一些事情,使控制远离任何代码,无论它在做什么。这就是信号的作用。为此,您将不得不使用pthread_kill(),或者其他类似的方法。

模式应该是

  1. 检测到错误的线程需要以某种方式将该错误通信回主线程。
  2. 主线程需要调用pthread_kill()来处理所有剩余的线程。不要被它的名字弄糊涂了——pthread_kill()只是一种向线程传递任意信号的方法。请注意,像STOP, CONTINUE和TERMINATE这样的信号即使使用pthread_kill()引发也是进程范围的,而不是特定于线程的,所以不要使用它们。在每个线程中,你都需要一个信号处理程序。在将信号传递给线程时,无论长时间运行的函数在做什么,该线程中的执行路径都将跳转到处理程序。你现在回到(有限的)控制,并且可以(可能,好吧,也许)做一些有限的清理和终止线程。在此期间,主线程将调用pthread_join()在所有的线程它的信号,这些现在将返回。

我的想法:

  • 这是一个非常丑陋的方式(信号/线程是出了名的难以得到正确,我不是专家),但我真的不知道你有什么其他的选择。
  • 在源代码中看起来"优雅"还有很长的路要走,尽管最终用户体验将是OK的。
  • 你将在运行库函数的过程中中止执行,所以如果有任何清理,它通常会做(例如释放它已经分配的内存),这将不会完成,你将有内存泄漏。在valgrind之类的东西下运行是一种确定是否发生这种情况的方法。
  • 让库函数清理(如果需要的话)的唯一方法是让你的信号处理程序将控制返回给函数并让它运行到完成,这正是你不想做的。
  • 当然,这在Windows上不起作用(没有pthread,至少没有值得一提的,尽管可能有一个等效的机制)。

最好的方法是重新实现(如果可能的话)那个库函数。