释放Pthread中的指针
我想制作结构的副本,然后将其传递到pthread,然后在pthread线程中fileny将其释放。但是我无法将其弄出来,这只是在这一点上远远超出了我,我找不到有关传递克隆结构并在线程中释放它们的任何教程。伪代码以下:
typedef struct unit_t { int id; char value[20]; char pid[20]; char id[100]; int type; }
pthread_t th;
void* myThread(void *arg){
how do i print a struct value and free arg in here?
free(arg);
}
void someFunction(*unit){
//create a local copy of struct
unit_t *tmp = malloc(sizeof(struct unit_t));
//clone initial *unit
*tmp = *unit;
//spawn thread
if(pthread_create(&th, NULL, &myThread, &tmp) != 0) {
bwlog("failed to create thread");
//free tmp if thread failed
free(tmp);
}else{
pthread_detach(th);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
void *
可以隐式转换为任何指针类型。但是,请注意,通过
& tmp
(用自动存储持续时间)在您的代码中很危险,因为您不能确保 tmp 的nofollow noreferrer“> lifetime 匹配创建线程的寿命。换句话说,
& tmp
指向somefunction
中的本地变量。somefunction
可能会在线程完成执行之前返回,在这种情况下,arg
将是悬空指针。您要传递
tmp
的值(分配的存储持续时间)。还请注意,函数的地址(
& mythread
)具有与函数本身相同的值(mythread
)。运营商的地址在这里是多余的。一个粗略的示例,没有任何错误处理,它连续产生了动态分配的内存的线程。
必须按照您的“克隆”结构来注意。这将被视为A 浅副本,为结构的字节副本执行字节。
虽然这对于您定义的结构很好,但是当结构包含指针(或嵌套指针)时,就会出现问题。这种结构的浅副本将导致两个结构都将相同的指针固定为一个对象。
像以前一样,当涉及线程时,指向对象的寿命可能与线程的寿命不符,或者指针是动态分配的内存,(难以回答的问题 释放的对象?
可能需要
A
void *
can be implicitly converted to any pointer type.Note, however, that passing
&tmp
(the address of an object with automatic storage duration) is dangerous in your code, since you do not ensure that the lifetime oftmp
matches the lifetime of the created thread.In other words,
&tmp
points to a local variable insomeFunction
.someFunction
may return before the thread finishes its execution, in which casearg
will be a dangling pointer.You want to pass the value of
tmp
(the address of an object with allocated storage duration).Note also that the address of a function (
&myThread
) has same value as the function itself (myThread
). The address-of operator is superfluous here.A cursory example, with no error handling, that continuously spawns threads that are passed dynamically allocated memory.
Care must be taken with "cloning" a structure the way you have. This would be considered a shallow copy, performing a byte for byte copy of the structure.
While this is fine for the structure you have defined, a problem arises when a structure contains a pointer (or nested pointers). A shallow copy of such a structure would result in both structures holding the same pointer to a single object.
Like before, when threads are involved, the pointed-to-object's lifetime may not match the lifetime of a thread, or if the pointer is to dynamically allocated memory, the (difficult) question to answer becomes Where and when is the object freed?
A deep copy may be needed.
您确实说过您的帖子是伪代码,但有一些问题。
typedef
,并且有两个具有相同名称的字段:malloc
的返回值,tmp ,它已经是指针。
free
it。您还应该适当地退出功能:我不知道您在做什么的范围,但是我用
pthread_detach
pthread_join
只是为了确保所有内容都在执行之前该过程终止。您可以找到一个有效的演示在这里。You did say your post is pseudo code, but it has a few problems.
typedef
, and you have two fields with the same name:malloc
tmp
, it's already a pointer.free
it. You should also exit the function appropriately:I don't know the larger scope of what you're doing, but I replaced
pthread_detach
withpthread_join
just to make sure everything executes before the process terminates. You can find a working demonstration here.