在 pthread_create() 中使用 locals 作为参数是否有效? [英] Does using locals as arguments in pthread_create() work?

查看:91
本文介绍了在 pthread_create() 中使用 locals 作为参数是否有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这主要是关于作用域和线程的问题.假设我们有以下结构.

This is mainly a question about scope and threads. Let's say we have the following struct.

struct Test
{
  int number;
  std::string name;
};

此结构的一个实例将用作 pthread_create 函数中的参数.下面是这可能是什么样子的示例.

An instance of this struct will be used as an argument in the pthread_create function. Here is an example of what this might look like.

pthread_t tid[5];
for(int i = 0; i < 5; ++i)
{
  Test test;
  test.number = 5;
  test.name = "test";
  pthread_create(&tid[i], NULL, func, (void *)&test);
}

这可以接受吗?由于 test 是在 for 的作用域中声明的,这意味着我们只能依赖它在 for 循环的单次迭代中存在.

Is this acceptable? Since test is declared in the for's scope, that means we can only rely on it existing during a single iteration of the for loop.

当 pthread_create 被调用时,一个指向 test 的指针作为参数给出.这意味着 func 正在接收传递给 pthread_create 的相同指针.这意味着当 test 超出范围时,我们不能再依赖那个指向 test 的指针.如果我们要创建更多的局部变量,从而改变堆栈,指针指向的位置将被那些新的局部变量覆盖.这样对吗?

When pthread_create is called, a pointer to test is given as an argument. This means that func is receiving the same pointer that is passed in to pthread_create. This means that when test goes out of scope, we can no longer rely on that pointer referring to test. If we were to create more locals, hence changing the stack, the location that the pointer points to would be overwritten by those new locals. Is this correct?

推荐答案

这可以接受吗?

一般来说,不会,因为你说的原因.具体来说,线程的入口函数可能直到 Test 对象被销毁后才开始执行,或者当线程的入口函数仍在使用它时,Test 对象可能会被销毁.这两种可能性中的任何一种都会导致未定义的行为(以及一个只能有时"正确运行的程序).

In general, no, for the reasons you stated. Specifically, the thread's entry-function may not start executing until after the Test object has been destroyed, or the Test object might be destroyed while the thread's entry-function is still using it. Either of those possibilities will lead to undefined behavior (and a program that only works correctly "sometimes").

你需要保证你传递给线程的数据在线程需要使用的时候一直有效.一种方法是在堆上分配该 Test 对象,并在使用完 Test 指针后让线程调用 delete .或者,您可以使用条件变量来暂停"主线程,直到子 pthread 发出信号表示它已完成访问主线程堆栈上的测试,以便主线程现在可以安全地继续执行.

You need to guarantee that the data you pass to the thread remains valid for as long as the thread needs to use it. One way to do that is to allocate that Test object on the heap, and have the thread call delete on the Test-pointer when it's done using it. Alternatively, you could use a condition variable to "pause" the main thread until the child pthread signals that it is done accessing the Test on the main thread's stack, so that it is now safe for the main thread to continue execution.

如果我们要创建更多的局部变量,从而改变堆栈,指针指向的位置将被那些新的覆盖当地人.这是正确的吗?

If we were to create more locals, hence changing the stack, the location that the pointer points to would be overwritten by those new locals. Is this correct?

是的,您正确理解了这个问题.

Yes, you understand the issue correctly.

这篇关于在 pthread_create() 中使用 locals 作为参数是否有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆