简体   繁体   English

当最后一个线程终止时,是否有任何pthread函数调用某些内容?

[英]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.

相关问题 哪个线程调用Windows proc回调函数? - Which thread calls the windows proc callback function? 当包含以下内容时,应用程序在启动时终止<boost/thread> - Application terminates on start when including <boost/thread> 当 C++ 程序在 Windows 上终止时,终止存根调用的最后一个函数是什么? - When a C++ program terminates on Windows what is the last function called by the termination stub? 如何在C ++中创建一个构造函数来创建pthread,它应该在线程开始运行时返回 - How to create a constructor in C++ for creating pthread which should return when thread starts running 线程本身终止时如何删除boost线程对象? - How to delete boost thread object when thread itself terminates? 当我在 linux 中使用分离的 pthread 时,我是否必须为线程函数返回 `NULL`? - Do I have to return `NULL` for thread function when I use the detached pthread in linux? pthread退出时运行功能 - Run function when pthread exits 如果pthread_cond_signal是线程中的最后一个调用,是否存在数据争用? - Is there a data race if pthread_cond_signal is the last call in a thread? C++:程序终止时执行函数 - C++: Execute function when program terminates 当父线程终止时,子线程是否退出 - Do child threads exit when the parent thread terminates
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM