为什么新创建的线程不通过 pthread_create() return 参数获取它的 tid,而是 pthread_self()
Why doesn't the newly created thread obtain its tid through pthread_create() return argument but pthread_self()
这是《Unix环境高级编程》中创建线程的示例代码。关于线程的创建,err = pthread_create(&ntid, NULL, thr_fn, NULL);
新创建的线程是否可以只使用ntid打印自己的threadID,而不是调用pthread_self()?
#include "apue.h"
#include <pthread.h>
pthread_t ntid;
void
printids(const char *s)
{
pid_t pid;
pthread_t tid;
pid = getpid();
tid = pthread_self();
printf("%s pid %u tid %u (0x%x)\n", s, (unsigned int)pid,
(unsigned int)tid, (unsigned int)tid);
}
void *
thr_fn(void *arg)
{
printids("new thread: "); /* for the newly created thread,can it
* print its own threadID just using ntid,
* instead of calling pthread_self()
*/
return((void *)0);
}
int
main(void)
{
int err;
err = pthread_create(&ntid, NULL, thr_fn, NULL);
if (err != 0)
err_quit("can't create thread: %s\n", strerror(err));
printids("main thread:");
sleep(1);
exit(0);
}
让我们检查规格:
http://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_create.html
pthread_create(pthread_t *restrict thread,
Upon successful completion, pthread_create() shall store the ID of the created thread in the location referenced by thread.
The calling thread can obtain the ID of the created thread through the return value of the pthread_create() function, and the newly created thread can obtain its ID by a call to pthread_self().
问题是:ntid
是调用函数的变量;即使它是全局的,从线程读取这个全局变量也不会扩展到两个或更多创建的线程。
其他问题是执行的时间和顺序。 pthread_create 将线程 ID 写入 ntid
在 新线程创建后:
Upon successful completion, pthread_create() shall store the ID of the created thread in the location referenced by thread.
因此,在没有同步的情况下从创建的线程读取 ntid
是不安全的,新线程可能会在实际值写入全局之前读取。
因此,您应该使用 pthread_self
:http://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_self.html
The pthread_self() function shall return the thread ID of the calling thread.
The pthread_self() function provides a capability similar to the getpid() function for processes and the rationale is the same: the creation call does not provide the thread ID to the created thread.
这是《Unix环境高级编程》中创建线程的示例代码。关于线程的创建,err = pthread_create(&ntid, NULL, thr_fn, NULL);
新创建的线程是否可以只使用ntid打印自己的threadID,而不是调用pthread_self()?
#include "apue.h"
#include <pthread.h>
pthread_t ntid;
void
printids(const char *s)
{
pid_t pid;
pthread_t tid;
pid = getpid();
tid = pthread_self();
printf("%s pid %u tid %u (0x%x)\n", s, (unsigned int)pid,
(unsigned int)tid, (unsigned int)tid);
}
void *
thr_fn(void *arg)
{
printids("new thread: "); /* for the newly created thread,can it
* print its own threadID just using ntid,
* instead of calling pthread_self()
*/
return((void *)0);
}
int
main(void)
{
int err;
err = pthread_create(&ntid, NULL, thr_fn, NULL);
if (err != 0)
err_quit("can't create thread: %s\n", strerror(err));
printids("main thread:");
sleep(1);
exit(0);
}
让我们检查规格:
http://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_create.html
pthread_create(pthread_t *restrict thread,
Upon successful completion, pthread_create() shall store the ID of the created thread in the location referenced by thread.
The calling thread can obtain the ID of the created thread through the return value of the pthread_create() function, and the newly created thread can obtain its ID by a call to pthread_self().
问题是:ntid
是调用函数的变量;即使它是全局的,从线程读取这个全局变量也不会扩展到两个或更多创建的线程。
其他问题是执行的时间和顺序。 pthread_create 将线程 ID 写入 ntid
在 新线程创建后:
Upon successful completion, pthread_create() shall store the ID of the created thread in the location referenced by thread.
因此,在没有同步的情况下从创建的线程读取 ntid
是不安全的,新线程可能会在实际值写入全局之前读取。
因此,您应该使用 pthread_self
:http://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_self.html
The pthread_self() function shall return the thread ID of the calling thread.
The pthread_self() function provides a capability similar to the getpid() function for processes and the rationale is the same: the creation call does not provide the thread ID to the created thread.