gpt4 book ai didi

c - 在C中的thread_join之后重用线程结构

转载 作者:行者123 更新时间:2023-11-30 15:20:17 25 4
gpt4 key购买 nike

我在堆栈上有一个线程变量数组,主线程稍后会加入该数组。我稍后必须再次运行相同数量的线程,并且想知道是否仍然可以使用相同的线程结构数组来创建新线程,或者线程结构是否不能再次用于创建新线程。

类似这样的事情:

main_method() {

pthread_t threads[10]
for (all threads)
pthread_create(&threads[i], NULL, some_func, &arg[i])

for (each thread in threads[10])
pthread_join(&threads[i]), ...)

// Does this work?
for (each thread in threads[10])
pthread_create(&threads[i], NULL, other_func, &arg[i])

//Or do I have to do this
pthread_t threads2[10]

for (each thread in threads2[10])
pthread_create(&threads2[i], NULL, other_func, &arg[i])

这里提出了类似的问题,但不确定答案是否解决了上述问题。

How to reuse threads using pthread_exit()

最佳答案

当一个线程退出时,它就完成了。你不能“重用”它——创建另一个。当然,您可以重用数组 - 但不能重用其中的值。 (即需要再次pthread_create)

您引用的问题是询问/并发/线程的最大数量 - OP 没有加入或没有让他的线程退出。在程序执行的生命周期内可以使用的线程数量没有限制。

关于c - 在C中的thread_join之后重用线程结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30109564/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com