I want all of the threads (in a multithreaded C code running on Linux) to call a function after a specific time . I tried alarm(). It is not thread safe: Only one SIGALRM generation can be scheduled in this manner. If the SIGALRM signal has not yet been generated, the call shall result in rescheduling the time at which the SIGALRM signal is generated.
Is there any way to implement such functionality and guarantee that the thread would call the function and leave its current task at that time?
我希望所有的线程(在Linux上运行的多线程C代码中)在特定时间之后调用一个函数。我试过闹钟()。它不是线程安全的:只能以这种方式安排一个SIGALRM生成。如果尚未生成SIGALRM信号,则呼叫将导致重新安排SIGALRM信号生成的时间。有没有办法实现这样的功能,并保证线程会调用该函数并在当时保留当前的任务?
1 个解决方案
#1
0
Do you have an array with all the pthread_t
s of the other threads? If so, iterate on the array and use pthread_kill
to send the signal to the other threads.
你有一个包含其他线程的所有pthread_ts的数组吗?如果是这样,请迭代数组并使用pthread_kill将信号发送到其他线程。
You can do this in the main thread, or in a separate thread.
您可以在主线程中或单独的线程中执行此操作。
Be careful that there's not much that you can safely do in a signal handler. No I/O except for write
, in particular.
请注意,在信号处理程序中可以安全地做的事情并不多。除了写入之外没有I / O,特别是。
#1
0
Do you have an array with all the pthread_t
s of the other threads? If so, iterate on the array and use pthread_kill
to send the signal to the other threads.
你有一个包含其他线程的所有pthread_ts的数组吗?如果是这样,请迭代数组并使用pthread_kill将信号发送到其他线程。
You can do this in the main thread, or in a separate thread.
您可以在主线程中或单独的线程中执行此操作。
Be careful that there's not much that you can safely do in a signal handler. No I/O except for write
, in particular.
请注意,在信号处理程序中可以安全地做的事情并不多。除了写入之外没有I / O,特别是。