我可以连续为已执行线程的新线程重用 pthread_t 和 pthread_attr_t 吗?
Can I reuse pthread_t and pthread_attr_t for new diferent thread of already executed thread consecutively?
我想在不使用 pthread_join 的情况下连续执行 2 个不同的线程,可以吗?或者我真的必须声明新的 thread_t 有点像这样:
pthread_create(&th,&thread_attr,shtdwn,(void*)&lpBuffer);
pthread_create(&th,&thread_attr,Run,(void*)&args);
而且我不需要等待任何一个线程完成。
非常感谢您的帮助!
每个 pthread_create
调用都可以重复使用 pthread_t
和 pthread_attr_t
变量。事实上, pthread_attr_t
像这样被重用是很常见的。但是,重新使用 pthread_t
变量有点不寻常,因为通常存储该值以用于线程上的后续 pthread
操作(例如 pthread_join
)。
此外,pthread_attr_t
可以为 NULL,在这种情况下将使用默认属性。但是 pthread_t
参数不能为 NULL。来自 pthread_create manual:
The attr argument points to a pthread_attr_t structure whose contents are used at thread creation time to determine attributes for the new
thread; this structure is initialized using pthread_attr_init(3) and
related functions. If attr is NULL, then the thread is created with
default attributes.
Before returning, a successful call to pthread_create() stores the ID
of the new thread in the buffer pointed to by thread;
我想在不使用 pthread_join 的情况下连续执行 2 个不同的线程,可以吗?或者我真的必须声明新的 thread_t 有点像这样:
pthread_create(&th,&thread_attr,shtdwn,(void*)&lpBuffer);
pthread_create(&th,&thread_attr,Run,(void*)&args);
而且我不需要等待任何一个线程完成。 非常感谢您的帮助!
每个 pthread_create
调用都可以重复使用 pthread_t
和 pthread_attr_t
变量。事实上, pthread_attr_t
像这样被重用是很常见的。但是,重新使用 pthread_t
变量有点不寻常,因为通常存储该值以用于线程上的后续 pthread
操作(例如 pthread_join
)。
此外,pthread_attr_t
可以为 NULL,在这种情况下将使用默认属性。但是 pthread_t
参数不能为 NULL。来自 pthread_create manual:
The attr argument points to a pthread_attr_t structure whose contents are used at thread creation time to determine attributes for the new thread; this structure is initialized using pthread_attr_init(3) and related functions. If attr is NULL, then the thread is created with default attributes.
Before returning, a successful call to pthread_create() stores the ID of the new thread in the buffer pointed to by thread;