pthread退出时运行函数 [英] Run function when pthread exits

查看:77
本文介绍了pthread退出时运行函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 C++ 应用程序,我在其中创建 pthread 来运行用户提供的函数.我希望能够在线程退出时以某种方式收到警报,以便我可以将其从用于保留线程的 pthread 数组中删除.有没有办法做到这一点,或者函数应该设置一些魔法值".因为我生成 pthread 的主要代码处于某种运行循环中,所以我可以轻松检查退出条件.

I have a C++ app in which I create pthreads to run user provided functions. I want to be able to be alerted in some way when a thread exits so that I can remove it from an array of pthread that I am using to keep the threads. Is there a way to do this, or should the function just set some "magic value". Because my main code that spawns the pthreads is in a sort of runloop, I can easily check for an exit condition.

此外,是否使用 std::vector 过度跟踪我的线程是否过载?线程数不一定是任何类型的常数,许多线程或很少的线程可能正在运行.或者是否有另一个 STL 容器适合这些添加和删除(添加总是在一端,删除几乎在任何地方).是否有其他结构可以跟踪 pthreads?堆栈或列表就在这里吗?还是一个标准的 C 数组,具有最大的好处?由于问题的性质,我还可以维护一个固定大小的工作线程数组,我将必须执行的用户函数传递给这些工作线程.这是一个好的解决方案吗?

Also, is using a std::vector<pthread_t> overdoing to keep track of my threads an overload? The number of threads is not necessarily any sort of constant, many threads or very few could be running. Or is there another STL container that would be good for these additions and deletions (additions always at one end, deletions almost anywhere). Is there some other structure for keeping track of pthreads? Would a stack or a list be right here? Or a standard C array with a generous maximum good? Due to the nature of the problem, I could also maintain a fixed size array of worker threads to whom I pass the user functions that must be executed. Is this a good solution?

抱歉,这个问题很长很困惑,但我只使用过动态语言中的线程,这永远不会成为问题.

Sorry for the long confused question, but I have only worked with threading in dynamic languages where this would never be an issue.

编辑 (3/08/12):阅读@jojojapan 的回答后,我决定使用各种线程池.在我的结构中,我有一个生产者(runloop 中的一个线程)和许多消费者(池中的工作线程).是否有为多线程单生产者多消费者使用而制作的数据结构?或者我可以只使用带有 pthread_mutex_tstd::queue 吗?

EDIT (3/08/12): After reading @jojojapan's answer, I have decided to use a threadpool of sorts. In my structure, I have one producer (a thread in a runloop) and many consumers (the worker threads in the pool). Is there a data structure that is made for multithreaded one-producer many-consumer use? Or whould I just use a std::queue with a pthread_mutex_t on it?

推荐答案

  1. 您可能要考虑的一个选项是实际上不是在线程完成任务后结束和删除线程,而是让它们保持活动状态并让它们等待新任务分配给他们.你可以通过做两件事来实现:

  1. One option you might want to consider is to not actually end and delete threads once they finished a task, but instead keep them alive and have them wait for a new task to be assigned to them. You can accomplish this by doing two things:

  1. 在线程中使用(几乎)无限循环
  2. 使用并发队列或其他一些技术使它们等待另一个线程发出的信号.在几个 SO 问题中讨论了设计模式和策略,例如这个

  • 如果你真的想在线程结束后发送信号,你可以使用 pthread_cond_t 并在其上调用 pthread_cond_signal就在线程到达它的 return 语句之前.当然,这假设有一些其他线程正在运行,等待这些信号并通过从向量中删除相应的线程来对它们进行操作.有关用法的详细信息在相应的手册页中进行了描述,但也在此 SO 帖子中进行了描述.

  • If you really want to send a signal once a thread ends, you can use a pthread_cond_t and call pthread_cond_signal on it just before a thread reaches its return statement. Of course that assumes that there is some other thread running that waits for these signals and acts upon them by removing the corresponding thread from the vector. Details on the usage are described on the corresponding man page, but also in this SO post.

    编辑与评论和问题的编辑部分相关:

    Edit related to the comment and the edited part of the question:

    1. 关于工作线程的数量:这取决于线程使用最多的资源.如果这些线程所做的主要是计算和一些内存访问,换句话说,如果它们受 CPU 限制,那么使用 CPU 可以维护的尽可能多的线程是有意义的(具体来说,有一定数量的内核,和每个内核的(硬件)线程数,您的 CPU 可以在它们开始相互减慢之前运行.您正在创建的线程(软件线程)应该大约一样多,或者可能更多(最多两倍于根据@Tudor 在这里所说的),硬件线程是合理的.但是,如果您的线程大量使用内存(内存绑定)或硬盘(IO 绑定)或其他资源,例如网络、NFS 或某些其他服务器,您可能希望按顺序减少线程数(a) 不会导致它们相互阻塞,并且 (b) 不会对某些资源施加不合理的过多负载.确定正确的线程数可能需要进行试验,而保持数量可配置通常是个好主意.

    1. Regarding the number of worker threads: That depends on the resources used the most by the threads. If what those threads do is mostly computation and a bit of memory access, in other words, if they are CPU-bound, it makes sense to use as many threads as your CPU can maintain (specifically, there is a certain number of cores, and number of (hardware) threads per core that your CPU can run before they start slowing each other down. The threads you are creating (software threads) should be about as many, or perhaps a few more (up to two times as many as hardware threads is reasonable according to what @Tudor says here)). However, if your threads make heavy use of memory (memory-bound) or harddisk (IO-bound) or other resources such as the network, NFS, or some other server, you might want to reduce the number of threads in order (a) not to cause them to block each other, and (b) not to put unreasonably much load on certain resources. Determining the right number of threads may be a matter of experimenting, and keeping the number configurable is generally a good idea.

    关于存储工作任务的最佳数据结构:并发有界队列 在我上面进一步引用的帖子的评论中提到的可能非常好.不过我自己没试过.但是如果你想让事情变得简单,一个标准的 std::queue,甚至只是一个 std::vector 都不是一个糟糕的选择,如果你保护他们正确使用信号/互斥体技术.

    Regarding the best data structure to store work tasks: The concurrent bounded queue mentioned in the comments of the post I cited further above is probably very good. I haven't tried it myself, though. But if you'd like to keep things simple, a standard std::queue, or even simply a std::vector would not be a bad choice, if you protect them properly using the signal/mutex technique.

    这篇关于pthread退出时运行函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆