当我在 linux 中使用分离的 pthread 时,线程函数是否必须 return `NULL`?
Do I have to return `NULL` for thread function when I use the detached pthread in linux?
例如:
void* thread(void *arg)
{
return NULL;
}
int main()
{
pthread_attr_t attr;
pthread_t th;
pthread_attr_setdetachstate(pthread_attr_t &attr, PTHREAD_CREATE_DETACHED);
pthread_create(&th, &attr, thread, NULL);
}
因为它是一个分离的线程,我无法接收到 pthread_join
返回的函数。所以我觉得thread的return没用,是不是应该returnNULL
?如果我不这样做 return NULL
,会不会导致一些错误?
should I return NULL?
您可以 return nullptr
或任何其他值,无论线程是否分离。
If I don't return NULL, will it cause some bug?
如果您不return任何东西,那么程序的行为将是不确定的。
P.S。 C++ 标准库具有可移植的线程包装器,可让您不依赖于任何特定的操作系统。我建议使用那些而不是系统特定的 API.
例如:
void* thread(void *arg)
{
return NULL;
}
int main()
{
pthread_attr_t attr;
pthread_t th;
pthread_attr_setdetachstate(pthread_attr_t &attr, PTHREAD_CREATE_DETACHED);
pthread_create(&th, &attr, thread, NULL);
}
因为它是一个分离的线程,我无法接收到 pthread_join
返回的函数。所以我觉得thread的return没用,是不是应该returnNULL
?如果我不这样做 return NULL
,会不会导致一些错误?
should I return NULL?
您可以 return nullptr
或任何其他值,无论线程是否分离。
If I don't return NULL, will it cause some bug?
如果您不return任何东西,那么程序的行为将是不确定的。
P.S。 C++ 标准库具有可移植的线程包装器,可让您不依赖于任何特定的操作系统。我建议使用那些而不是系统特定的 API.