将多个参数从结构传递给线程
我在传递参数时遇到问题,使用包含另一个结构的结构
我知道我使用结构的方式有问题,但我只是看不到哪里......
谢谢!
这是我的结构
typedef struct {
IMAGE *imagenfte;
IMAGE *imagendst;
}thread_data;
//thread_data *data = (thread_data *) malloc(sizeof(thread_data));
这是另一个结构
typedef struct {
HEADER header;
INFOHEADER infoheader;
PIXEL *pixel;
} IMAGE;
IMAGE imagenfte,imagendst;
这是我的线程函数
void *processBMP2(void *argumentos)
{
thread_data *my_data;
my_data = (thread_data *) (argumentos);
IMAGE *imagefte, *imagedst;
imagefte = my_data->imagenfte;
imagedst = my_data->imagendst;
free(my_data);
int i,j;
int count=0;
PIXEL *pfte,*pdst;
PIXEL *v0,*v1,*v2,*v3,*v4,*v5,*v6,*v7;
int imageRows,imageCols;
memcpy(imagedst,imagefte,sizeof(IMAGE)-sizeof(PIXEL *));
imageRows = imagefte->infoheader.rows;
imageCols = imagefte->infoheader.cols;
imagedst->pixel=(PIXEL *)malloc(sizeof(PIXEL)*imageRows*imageCols);
...
这是我创建线程和传递参数的方式
pthread_t hilo;
thread_data *my_data = (thread_data *) malloc(sizeof(thread_data));
my_data->imagenfte = &imagenfte;
my_data->imagendst = &imagendst;
pthread_create(&hilo,NULL, processBMP2, my_data);
//processBMP(&imagenfte,&imagendst);
I'm having problems passing the arguments, using a structure containing another structure
i know there's something wrong with the way i'm using the structures but i just cant see where...
Thanks!
this is my struct
typedef struct {
IMAGE *imagenfte;
IMAGE *imagendst;
}thread_data;
//thread_data *data = (thread_data *) malloc(sizeof(thread_data));
this is the other structure
typedef struct {
HEADER header;
INFOHEADER infoheader;
PIXEL *pixel;
} IMAGE;
IMAGE imagenfte,imagendst;
this is my thread function
void *processBMP2(void *argumentos)
{
thread_data *my_data;
my_data = (thread_data *) (argumentos);
IMAGE *imagefte, *imagedst;
imagefte = my_data->imagenfte;
imagedst = my_data->imagendst;
free(my_data);
int i,j;
int count=0;
PIXEL *pfte,*pdst;
PIXEL *v0,*v1,*v2,*v3,*v4,*v5,*v6,*v7;
int imageRows,imageCols;
memcpy(imagedst,imagefte,sizeof(IMAGE)-sizeof(PIXEL *));
imageRows = imagefte->infoheader.rows;
imageCols = imagefte->infoheader.cols;
imagedst->pixel=(PIXEL *)malloc(sizeof(PIXEL)*imageRows*imageCols);
...
and this is the way i`m creating the thread and passing de arguments
pthread_t hilo;
thread_data *my_data = (thread_data *) malloc(sizeof(thread_data));
my_data->imagenfte = &imagenfte;
my_data->imagendst = &imagendst;
pthread_create(&hilo,NULL, processBMP2, my_data);
//processBMP(&imagenfte,&imagendst);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你正在做的事情是完全正确的。新线程需要负责释放内存,因为父线程无法知道新线程何时完成对内存的访问。
What you're doing is exactly right. The new thread needs to be the one responsible for freeing the memory because the parent thread cannot know when the new thread is done accessing it.