[英]Is there any pthread function which calls something when the last thread terminates?
Suppose I have created 10 pthreads using a for
loop in a C++ application on Linux. 假设我在Linux上的C ++应用程序中使用
for
循环创建了10个pthread。 All these 10 threads are calling the same thread function. 所有这10个线程都调用相同的线程函数。 I want the last exiting thread (between [1..10]) to call a function where I can clear some data.
我希望最后一个退出线程(在[1..10]之间)调用一个可以清除一些数据的函数。 Is there any pthread function like that?
有没有这样的pthread函数? (Not talking about thread specific data and
pthread_once()
) (不谈论线程特定的数据和
pthread_once()
)
Note: Requirement is not with std::thread
here. 注意:此处的要求不是
std::thread
要求。
If you want the last thread that exits to perform the work then have all 10 threads decrement a counter that starts off set to the number of threads. 如果要退出的最后一个线程执行工作,则使所有10个线程递减,将一个开始计数的计数器设置为线程数。 If you're using
std::atomic
then when you do a fetch_sub
and get back the value 1
you'll know you are the last thread running and can perform the work: 如果您使用的是
std::atomic
那么当您执行fetch_sub
并返回值1
您将知道您是最后一个正在运行的线程并可以执行工作:
int threads_active = atomic_threads_active.fetch_sub(1);
if (threads_active == 1)
{
// We are the last thread of the 10 that were launched.
clearSomeData();
}
I don't know about a specific function for that, but you can simply join all 10 threads in a loop. 我不知道具体的功能,但是您可以简单地将所有10个线程连接成一个循环。 Once all threads have joined, you can execute the function.
一旦所有线程都加入,就可以执行该功能。 You can do this in the thread that spawned the 10 threads or in a separate thread if you so prefer.
您可以在产生10个线程的线程中执行此操作,也可以根据需要在单独的线程中执行此操作。
i had to detach all the threads
我不得不拆开所有的螺纹
That's not a very good choice if you want to wait for them to exit. 如果您要等他们退出,那不是一个很好的选择。
But you can work around it by using a shared array of 10 condition variables. 但是您可以通过使用10个条件变量的共享数组来解决此问题。 Each of the threads should set the variable corresponding to it and call
pthread_cond_signal
. 每个线程都应设置与其对应的变量,然后调用
pthread_cond_signal
。 The waiter should use pthread_cond_wait
in a loop to wait for each of the condition variables to be set. 服务员应在循环中使用
pthread_cond_wait
来等待每个条件变量的设置。
In this case, main() calls func1(), func1() creates a pthread_t pointer which points to arrays of pthread_ts( pthread_t *tid = new pthread_t[10]).
在这种情况下,main()调用func1(),func1()创建一个pthread_t指针,该指针指向pthread_ts的数组(pthread_t * tid = new pthread_t [10])。 And creates 10 threads in for loop.
并在for循环中创建10个线程。 After creation, control will be returned to main(), main() has other tasks to do.
创建后,控制权将返回给main(),main()还有其他任务要做。 Now, the problem is how can i remove (tid) from heap memory?
现在,问题是如何从堆内存中删除(TID)?
You can simply do as I suggested. 您可以按照我的建议做。 Don't detatch the threads, but wait for the threads to join in a loop.
不要分离线程,但要等待线程加入循环。 Once they're finished, you are free to destroy the thread objects.
完成后,您可以随意销毁线程对象。 You can do this in a separate thread as I said.
如我所说,您可以在单独的线程中执行此操作。 No need for the main thread to wait anything.
不需要主线程等待任何东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.