RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 765444
Accepted
yrHeTateJlb
yrHeTateJlb
Asked:2021-01-01 03:41:21 +0000 UTC2021-01-01 03:41:21 +0000 UTC 2021-01-01 03:41:21 +0000 UTC

阻塞进程直到线程/子进程退出

  • 772

有这个代码:

if(pid_t pid = fork()) // spawn child process
{
    // parent process
    LOG("Try to execute smth. in child process");
    return;
}
else
{
    // child process
    doWork();
    std::terminate();
}

这里产生了一个子进程,其中执行了一些工作。在其他地方(即在流程结束时)有这样的:

// wait childern(some code may call fork())
const pid_t pid{::wait(nullptr)};

在这里,该进程正在等待子进程(之前分叉的)终止。所以,所有这些乐趣都不能与 key 一起使用-fsanintize=thread。代码挂起是wait因为编译器添加了一些不会自行终止的子进程。

问题。如何在没有 的情况下实现类似的行为wait?这样任务异步运行,主进程在终止时等待这些任务完成。也许使用线程,而不是进程。如果我有 Qt 可供我使用,我会将它们doWork放入QThreadPool其中,他会等待析构函数中所有任务的完成。但我没有 Qt,但我有 C++14 和 pthread。有人可以提出任何解决方案吗?

c++
  • 2 2 个回答
  • 10 Views

2 个回答

  • Voted
  1. koresh
    2020-01-03T14:43:53Z2020-01-03T14:43:53Z

    您可以使用std::thread:

    #include <iostream>       
    #include <thread>        
    
    void func1() //Функция для запуска в потоке
    {
        std::cout << "Executing func1" << std::endl;
    }
    
    void func2(int x) //Еще одна функция с аргументом
    {
        std::cout << "Executing func2 with x=" << x << std::endl;
    }
    
    int main() 
    {
      //Запускаем функции в отдельных потоках
      std::thread thread1 (func1);
      std::thread thread2 (func2,1);
    
      //Параллельно можем исполнять код в главном процессе    
      std::cout << "Execute some commands in main process" << std::endl;
    
      //Ждем завершения кода в потоках
      thread1.join();
      thread2.join();
    
      std::cout << "Thread execution completed" << std::endl;
    
      return 0;
    }
    

    此外,要并行化该过程,您可以使用std::async:

    #include <cstdlib>
    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <numeric>
    #include <future>
    #include <chrono>
    
    //Некоторая задача которую мы будем выполнять асинхронно
    template <typename RandomIt>
    int parallel_sum(RandomIt beg, RandomIt end)
    {
        auto len = end - beg; //определяем длину вектора
        if (len < 1000)
            return std::accumulate(beg, end, 0); //короткий вектор суммируем сразу
        //Рекурсивно запускаем parallel_sum для получения большего числа потоков
        RandomIt mid = beg + len/2;
        auto handle = std::async(std::launch::async, parallel_sum<RandomIt>, mid, end);
    
        int sum = parallel_sum(beg, mid);
        return sum + handle.get();
    }
    
    int main()
    {
        //Для примера будем суммировать большой vector случайных чисел
        std::vector<int> v(100000);
        for (auto& e: v){
            e = std::rand();
        }
        //Для интереса сравним время выполнения суммирования вектора с помощью async: 
        auto t0 = std::chrono::high_resolution_clock::now();
        std::cout << "The sum is " << parallel_sum(v.begin(), v.end()) << '\n';
        auto t1 = std::chrono::high_resolution_clock::now();
        std::cout << std::chrono::duration_cast<std::chrono::milliseconds>(t1-t0).count() << "msec\n";
    
        //и суммирование в одном главном потоке:
        t0 = std::chrono::high_resolution_clock::now();
        std::cout << std::accumulate(v.begin(), v.end(), 0);
        t1 = std::chrono::high_resolution_clock::now();
        std::cout << std::chrono::duration_cast<std::chrono::milliseconds>(t1-t0).count() << "msec\n";
    
    }
    

    我得到以下结果:

    The sum is 1746632618
    9msec
    17466326181msec
    

    std::async- 高级工具,它不保证代码一定会在单独的线程中执行。

    接下来,我举一个线程管理最简单的例子,虽然这是一个完全不同的故事,但我强调这是最简单的例子,还有很多需要解决的,比如所有线程共享cout:

    #include <iostream>
    #include <thread>
    #include <vector>
    
    using namespace std;
    
    void func(const int i) { // Функция, исполняемая в потоках
        cout << "Thread N " << i << " created" << endl;
    }
    
    int main() {
        vector<thread> thread_pool; //создаем пул потоков на основе вектора
        int threadNum = 10; //количество потоков
        for (int i=0; i < threadNum; i++) {
            thread_pool.push_back(thread(func,i)); //запускаем потоки
        }
        for (auto& t : thread_pool) {
            t.join(); //ожидаем окончание выполнения потоков
        }
    }
    
    • 3
  2. Best Answer
    Croessmah stands with Russia
    2020-01-06T22:33:29Z2020-01-06T22:33:29Z

    我匆忙把它扔了进去,但我认为这个想法很清楚。

    #include <sys/types.h>
    #include <sys/wait.h>
    #include <unistd.h>
    #include <vector>
    #include <chrono>
    #include <thread>
    #include <random>
    #include <iostream>
    
    class fork_storage
    {
    public:
        fork_storage() noexcept
        {
            m_pid = fork();
        }
    
        fork_storage(const fork_storage&) = delete;
        fork_storage& operator=(const fork_storage&) = delete;
        fork_storage& operator=(fork_storage&& rhv) = delete;
    
        fork_storage(fork_storage&& src) noexcept
        {
            m_pid = src.pid();
            src.reset();
        }
    
        ~fork_storage()
        {
            if (is_parent()) {
                wait();
            }
        }
    
        bool fail() const noexcept
        {
            return m_pid < 0;
        }
    
        bool is_parent() const noexcept
        {
            return m_pid > 0;
        }
    
        void reset() noexcept
        {
            m_pid = -1;
        }
    
        pid_t pid() const noexcept
        {
            return m_pid;
        }
    
        int wait() noexcept
        {
            int status = -1;
            if (is_parent()) {
                waitpid(m_pid, &status, WUNTRACED);
            }
            reset();
            return status;
        }
    
    
    private:
        pid_t m_pid;
    };
    
    
    
    std::mt19937 &random_gen()
    {
        static std::mt19937 gen(std::chrono::system_clock::now().time_since_epoch().count());
        return gen;
    }
    
    
    
    
    int main()
    {
        std::vector<fork_storage> processes;
        for (unsigned i = 0; i < 10; ++i) {
            fork_storage fork_obj;        
            if (fork_obj.fail()) {
                std::cerr << "fork error\n";
            } else {
                if (fork_obj.is_parent()) {
                    std::string message = "process created: " + std::to_string(fork_obj.pid()) + "\n";
                    processes.emplace_back(std::move(fork_obj));
                } else {
                    for (auto &process: processes) {
                        process.reset();
                    }
                    processes.clear();
                    std::chrono::seconds sec(random_gen()() % 5 + 1);
                    std::string message = "child " + std::to_string(getpid()) + " paused: " + std::to_string(sec.count()) + " sec\n";
                    std::cout << message;
                    std::this_thread::sleep_for(sec);
                    break;
                }
            }
        }
    
        while (!processes.empty()) {
            fork_storage &fork_obj = processes.back();
            if(fork_obj.is_parent()) {
                std::string message = "wait process: " + std::to_string(fork_obj.pid()) + "\n";
                std::cout << message;            
            }
            processes.pop_back();
        }    
    }
    

    http://rextester.com/LUZI37730

    • 2

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    是否可以在 C++ 中继承类 <---> 结构?

    • 2 个回答
  • Marko Smith

    这种神经网络架构适合文本分类吗?

    • 1 个回答
  • Marko Smith

    为什么分配的工作方式不同?

    • 3 个回答
  • Marko Smith

    控制台中的光标坐标

    • 1 个回答
  • Marko Smith

    如何在 C++ 中删除类的实例?

    • 4 个回答
  • Marko Smith

    点是否属于线段的问题

    • 2 个回答
  • Marko Smith

    json结构错误

    • 1 个回答
  • Marko Smith

    ServiceWorker 中的“获取”事件

    • 1 个回答
  • Marko Smith

    c ++控制台应用程序exe文件[重复]

    • 1 个回答
  • Marko Smith

    按多列从sql表中选择

    • 1 个回答
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Suvitruf - Andrei Apanasik 什么是空? 2020-08-21 01:48:09 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5