Pat*_*ins 4 c multithreading pthreads lifetime
签名pthread_create(3)是:
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg);
Run Code Online (Sandbox Code Playgroud)
pthread_t *thread论证的存储持续时间有哪些要求?手册页pthread_create说:
在返回之前,成功调用
pthread_create()将新线程的ID存储在指向的缓冲区中thread;
但如果这意味着它存储的值有这样调用者可以检查它,或者如果它使用了缓冲存储的值(这意味着缓冲区需要保持可用的子线程的整个生命周期),目前还不清楚.
同样,pthread_self它说它返回
在创建此线程
*thread的pthread_create(3)调用中返回的值相同.
但是不清楚它是否意味着它返回存储的值*thread或者等于*thread返回的值.
具体来说,我想知道这样的事情是否合法:
void make_thread(void) {
pthread_t tid;
return pthread_create(&tid, NULL, some_fn, NULL);
}
Run Code Online (Sandbox Code Playgroud)
或者如果tid需要malloc'd.当我放入tid堆栈时,我在valgrind中遇到了一堆错误_Unwind_ForcedUnwind,这让我怀疑*thread需要在子线程的生命周期内保持有效.
返回线程ID供您自己使用.如果您要分离线程或者线程将自行分离,则无需存储它.
void make_thread(void) {
pthread_t tid;
return pthread_create(&tid, NULL, some_fn, NULL);
}
Run Code Online (Sandbox Code Playgroud)
这有点奇怪.您无法加入该主题,因为您没有保留其ID.你没有脱掉它.我认为如果线程自行分离可能会很好,但这是一种奇怪的做事方式.
| 归档时间: |
|
| 查看次数: |
237 次 |
| 最近记录: |