关于 pthread_barrier_wait
我正在使用 pthread_barrier_wait 来同步线程,但在我的程序中,一个或多个线程可能会在其他线程等待它们到达 pthread_barrier_wait 时过期。现在有没有办法让陷入 pthread_barrier_wait 的线程知道某些线程已过期,而所有线程都已到达屏障?
I'm using pthread_barrier_wait to synchronize threads, but in my program there is a possibility that one or more of the threads expire while others are waiting for them to reach pthread_barrier_wait. Now is there a way, that threads stuck at pthread_barrier_wait know that some of the threads have expired while all have reached the barrier?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这在很大程度上取决于它们过期的方式和原因。
屏障并不关心在何处调用 pthread_barrier_wait(),因此如果它是编程到期,则只需在此时调用 wait 即可。屏障计数器会递减,当线程被释放时,您可以执行正常的错误检查,然后立即调用 pthread_exit 或其他。将 pthread_wait 放在单独的函数中可能会简化事情。
如果线程由于被杀死或取消而过期,那么生活会更加复杂,您可能会进入巨大的黑客领域,可能值得重新考虑设计。
That depends a lot on how and why they expire.
The barrier doesn't care where pthread_barrier_wait() is called on it so if it's a programmed expiration then just call wait on it at that point. The barrier counter gets decremented and when the threads are released you can do the normal error checking and then just immediately call pthread_exit or whatever. Putting the pthread_wait in a separate function might simplify things.
If the threads are expiring because they are getting killed or canceled then life is more complicated and you are probably headed into monumental hack territory and it might be worth reconsidering the design.