【发布时间】:2014-01-20 20:36:39
【问题描述】:
我有一个结构要传入 pthread_create 函数
例如
typedef struct INFO
{
int signal;
int group_id;
}info_type;
struct ARG
{
info_type *info;
int id;
};
int i;
static info_type info; //skipped how name and signal is initialized
struct ARG args[5];
for ( i = 0; i < 5; ++i)
{
args[i]->info = info;
id = i;
}
然后,我正在尝试创建一个将 ARG 指针作为 'arg' 的线程
启动例程就像
void task(struct ARG * arg)
{
while(arg->info->signal)
{
wait(arg->info->signal); // this part is just pseudo code (just waiting for sig)
}
// do some works
}
//this is pseudo code and assume pthread and attr are defined somewhere else
pthread(pthread, attr, task, &arg[0]);
所以,有多个参数,但只共享一个 info_type、相同的 group_id 和信号。 当共享信号发生变化时,我只希望它处理一个任务例程。
那么我的问题是:
传递给 pthread 函数的 arg 是否重要以检查信号是否已更改? 如果信号在 ex arg[3] 的另一个索引中更改, 这会在任务线程中检查还是根本无法识别?
因为每个 ARG 中的 'id' 都是必需的。那么如果 args[3] 改变了信号(arg[3] id necssary),这个任务会使用 arg[3] 吗?
或者它会使用我调用 pthread_create 函数时传递的任何 arg 参数吗?
【问题讨论】: