c++ - Do I have to return `NULL` for thread function when I use the detached pthread in linux?
问题描述
For example:
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);
}
Because of it's a detached thread, I can not receive the funtion retrun by pthread_join
. So I think the return of thread is useless, should I return NULL
? If I don't return NULL
, will it cause some bug?
解决方案
should I return NULL?
You can return nullptr
or any other value whether the thread is detached or not.
If I don't return NULL, will it cause some bug?
If you don't return anything then the behaviour of the program will be undefined.
P.S. The C++ standard library has portable wrappers for threads which allow you to not depend on any particular operating system. I recommend using those instead of system specific API.
推荐阅读
- arrays - 使用对象从数组发出 Laravel 请求
- java - WinAppDriver 如何在 Java 中定义一个 WindowsElement 而不会得到 java.lang.ClassCastException
- javascript - 正则表达式查找tml标签c#之外的任何文本
- python - 正则表达式提取包含三个元音的单词
- excel - 查找动态范围的列和行 VBA 的最小值、最大值和平均值
- c++ - 带有额外条件的 cin.fail C++
- c# - EF Core Navigation Property Include 使用左连接而不是内连接
- python - 为什么 fit_transform 总是将 'sex' 编码为 0?
- reactjs - 如何将 axios 拦截器与 Next.js 应用程序中的 redux 商店链接?
- angularjs - AngularJS:无法在 chrome 中调试