- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我偶然发现了一个代码示例 here .引起我注意的行(跳过所有其他行):
{
...
void *res;
...
s = pthread_join(tinfo[tnum].thread_id, &res);
...
free(res); /* Free memory allocated by thread */
}
有比我更深入了解 pthreads 的人可以对 free(res)
发表评论吗?我不得不说我以前从未见过这个,而且谷歌搜索 1-1.5 小时也没有给我任何其他类似的例子。
最佳答案
In pthread_join(thread_id, &res) , if &res is not NULL - is free(res) needed?
这取决于线程的返回值是否是动态分配的(with malloc()
& co)。
如果您查看同一页上的函数 thread_start()
,您会看到它有一个返回语句:
return uargv;
和uagrv
被分配了:
uargv = strdup(tinfo->argv_string);
因此,free()
调用在 pthread_join()
调用之后的 main()
中使用。因为 res
是用 uargv
填充的(由线程返回)。您可以在概念上假设 pthread_join()
函数中有这样一段代码:
if (res)
*res = uargv;
这是使用 strdup()
分配的(内部分配内存)。所以你 free()
它。如果线程只有 return NULL;
(并且 free() 是 uargv
本身),那么您不需要 free()
。
一般的答案是,如果您使用 malloc()
系列函数分配一些东西,那么您需要 free()
。
关于linux - pthread_join(thread_id, &res) ,如果 &res 不为 NULL - 是否需要 free(res) ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35770019/
mySQL 是否对用户连接使用特定的连接 ID (thread_id) 范围?我注意到我建立的所有连接都在 50 的范围内,例如 55,56。但是还有其他连接(不是我的)是 8 和 9。 或者是否有一
如何查询特定地址发送的所有消息? 经过多天的搜索,我无法在 StackOverflow 上找到我的答案。 我目前收到的所有消息都是这样的: Uri uri = Uri.parse("content:/
我在讨论这个话题Android get contact id from thread id有人和我问的问题完全一样。不幸的是,lopez.mikhael 的回答完全没有中标。 如何从 URI cont
我需要获取 mms-sms/conversations Android 内容提供商的 thread_id,这是我目前所做的: public long findThreadIdFromPhone
我很好奇线程是什么数据类型,以及它是否可分配给变量并且这样做是否可取/有用。 使用 #include 图书馆。 最佳答案 A std::thread是一个代表单个执行线程的类。 它本身不是操作系统线程
如果有任何“智能”方式可以获取进程内使用 pthread_created 创建的所有 threadID(假设这些线程是在第三方库中创建的),则使用 pthreads不会公开这些数据。 最佳答案 一种方
我发现短信数据库中有_id和thread_id两个字段,_id和thread_id有什么区别?如果我想通过 ID 删除短信,我应该使用哪个字段? _id 还是 thread_id ?谢谢! 最佳答案
我正在为 Pushbullet API 编写一个 swift 应用程序,用于 MacOS 上的短信通知和回复。 为了获取新的 SMS 消息,我正在监视 WebSocket wss://stream.p
给定 - 线程的线程 ID。 要求 - 设置线程 id 的 Linux 优先级。 约束 - 不能使用 setpriority() 我试过在下面使用 pthread_setschedprio(pthre
我偶然发现了一个代码示例 here .引起我注意的行(跳过所有其他行): { ... void *res; ... s = pthread_join(tinfo[tnum].thread_id
我是一名优秀的程序员,十分优秀!