从线程中定义的函数返回结构
Returning struct from function defined in thread
我有一个定义为
的结构
typedef struct{
int op;
int id;
int val;
} command_t;
command_t cmd_buffer[10];
我正在创建一堆线程来处理循环缓冲区中的结构数组。
在线程创建中,我调用函数 start_thread
,它调用 removefrombuffer
,它应该 return 一个结构,或者至少将一个结构传递给 start_thread 函数
void *start_thread (void *arg){
int i;
command_t item;
getfrombuffer(item);
///// bunch of operations from the item removed from buffer
}
command_t getfrombuffer (command_t item){
sem_wait(&fullpositions);
pthread_mutex_lock(&mutex);
item = cmd_buffer[buff_read_idx++];
if(buff_read_idx >= 6)
buff_read_idx=0;
pthread_mutex_unlock(&mutex);
sem_post(&freepositions);
return item;
}
不幸的是,在 start_thread
函数调用 getfrombuffer()
之后,在我尝试从那个 returned 项目或只是 item.id 的 printf 进行操作后,它会return 内存中的随机值,而不是从缓冲区中删除的值。
如何从这样的函数中正确地 return 结构?
问题是 item 是按值传递给您的函数的。
您可以通过两种方式更正:
- 使用简单的 return 值
void *start_thread (void *arg){
int i;
command_t item = getfrombuffer();
///// bunch of operations from the item removed from buffer
}
command_t getfrombuffer (void)
{
sem_wait(&fullpositions);
pthread_mutex_lock(&mutex);
command_t item = cmd_buffer[buff_read_idx++];
if(buff_read_idx >= 6)
buff_read_idx=0;
pthread_mutex_unlock(&mutex);
sem_post(&freepositions);
return item;
}
- 使用指针
void *start_thread (void *arg)
{
int i;
command_t item;
getfrombuffer(&item);
///// bunch of operations from the item removed from buffer
}
void getfrombuffer (command_t *item)
{
sem_wait(&fullpositions);
pthread_mutex_lock(&mutex);
*item = cmd_buffer[buff_read_idx++];
if(buff_read_idx >= 6)
buff_read_idx=0;
pthread_mutex_unlock(&mutex);
sem_post(&freepositions);
}
您从未将 *start_thread
函数中的 item
设置为任何值。将 getfromebuffer(item)
更改为 item = getfrombuffer(item)
。
另一个修复方法是将 getfrombuffer
更改为 void 类型并通过引用传递 item
(读取:传递指向 item
的指针),使您的代码如下:
void *start_thread (void *arg){
int i;
command_t item;
getfrombuffer(&item);
///// bunch of operations from the item removed from buffer
}
void getfrombuffer (command_t* item){
sem_wait(&fullpositions);
pthread_mutex_lock(&mutex);
*item = cmd_buffer[buff_read_idx++];
if(buff_read_idx >= 6)
buff_read_idx=0;
pthread_mutex_unlock(&mutex);
sem_post(&freepositions);
}
我有一个定义为
的结构typedef struct{
int op;
int id;
int val;
} command_t;
command_t cmd_buffer[10];
我正在创建一堆线程来处理循环缓冲区中的结构数组。
在线程创建中,我调用函数 start_thread
,它调用 removefrombuffer
,它应该 return 一个结构,或者至少将一个结构传递给 start_thread 函数
void *start_thread (void *arg){
int i;
command_t item;
getfrombuffer(item);
///// bunch of operations from the item removed from buffer
}
command_t getfrombuffer (command_t item){
sem_wait(&fullpositions);
pthread_mutex_lock(&mutex);
item = cmd_buffer[buff_read_idx++];
if(buff_read_idx >= 6)
buff_read_idx=0;
pthread_mutex_unlock(&mutex);
sem_post(&freepositions);
return item;
}
不幸的是,在 start_thread
函数调用 getfrombuffer()
之后,在我尝试从那个 returned 项目或只是 item.id 的 printf 进行操作后,它会return 内存中的随机值,而不是从缓冲区中删除的值。
如何从这样的函数中正确地 return 结构?
问题是 item 是按值传递给您的函数的。
您可以通过两种方式更正:
- 使用简单的 return 值
void *start_thread (void *arg){
int i;
command_t item = getfrombuffer();
///// bunch of operations from the item removed from buffer
}
command_t getfrombuffer (void)
{
sem_wait(&fullpositions);
pthread_mutex_lock(&mutex);
command_t item = cmd_buffer[buff_read_idx++];
if(buff_read_idx >= 6)
buff_read_idx=0;
pthread_mutex_unlock(&mutex);
sem_post(&freepositions);
return item;
}
- 使用指针
void *start_thread (void *arg)
{
int i;
command_t item;
getfrombuffer(&item);
///// bunch of operations from the item removed from buffer
}
void getfrombuffer (command_t *item)
{
sem_wait(&fullpositions);
pthread_mutex_lock(&mutex);
*item = cmd_buffer[buff_read_idx++];
if(buff_read_idx >= 6)
buff_read_idx=0;
pthread_mutex_unlock(&mutex);
sem_post(&freepositions);
}
您从未将 *start_thread
函数中的 item
设置为任何值。将 getfromebuffer(item)
更改为 item = getfrombuffer(item)
。
另一个修复方法是将 getfrombuffer
更改为 void 类型并通过引用传递 item
(读取:传递指向 item
的指针),使您的代码如下:
void *start_thread (void *arg){
int i;
command_t item;
getfrombuffer(&item);
///// bunch of operations from the item removed from buffer
}
void getfrombuffer (command_t* item){
sem_wait(&fullpositions);
pthread_mutex_lock(&mutex);
*item = cmd_buffer[buff_read_idx++];
if(buff_read_idx >= 6)
buff_read_idx=0;
pthread_mutex_unlock(&mutex);
sem_post(&freepositions);
}