pthread用于具有多个参数的进程

时间:2014-09-24 06:37:18

标签: c pthreads

我目前正在创建一个使用线程来处理BMP图像的程序。事情是......我知道pthread使用arg 4作为函数的签名......但是如果函数需要多于1个参数,我怎么能创建一个线程呢?

这是函数所需的结构:

typedef struct {
HEADER header;
INFOHEADER infoheader;
PIXEL *pixel;
} IMAGE;

IMAGE imagenfte,imagendst;

继承功能代码:

void *processBMP(IMAGE *imagefte, IMAGE *imagedst)
{
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);
for(i=1;i<imageRows-1;i++){
for(j=1;j<imageCols-1;j++)
{
pfte=imagefte->pixel+imageCols*i+j;
v0=pfte-imageCols-1;
v1=pfte-imageCols;
v2=pfte-imageCols+1;
v3=pfte-1;
v4=pfte+1;
v5=pfte+imageCols-1;
v6=pfte+imageCols;
v7=pfte+imageCols+1;
pdst=imagedst->pixel+imageCols*i+j;
...
}

...
int main()
{
int res;

///////Variables a utilizar en Hilo///////
int procBMP_t;  //Variable entera para la creación de los hilos
pthread_t proc_t;  //Hilo para el procesamiento del BMP
//////////////////////////////////////////

...
procBMP_t = pthread_create(&proc_t, NULL, processBMP, (void *) &imgsSENT);  //Hilo para el procesamiento de imagenes

我试着用这个来解决它:

struct threadImgs{   //Estructura para enviar ambas imagenes como argumento del thread
IMAGE fuente;
IMAGE destino;
};

struct threadImgs imgsSENT;


void *processBMP(void *imgs)
{

struct threadImgs* imagen = (struct threadImgs *) imgs;

IMAGE *imagefte = imagen->fuente;
IMAGE *imagedst = imagen->destino;
....
}

但它并没有真正的帮助,在初始化类型struct error&#34;

时,由于&#34;不兼容的类型甚至无法编译

任何帮助D:???

1 个答案:

答案 0 :(得分:1)

将两个参数包装到一个结构中的方法是可行的方法。

但请注意以下类型:

struct threadImgs
{   
  IMAGE fuente;
  IMAGE destino;
};

上面的结构将它的元素定义为IMAGE的实例。

您的线程函数尝试访问指向IMAGE的指针:

void *processBMP(void *imgs)
{
  struct threadImgs* imagen = (struct threadImgs *) imgs;

  IMAGE *imagefte = imagen->fuente;
  IMAGE *imagedst = imagen->destino;

  ...

因此要么更改结构以将其元素定义为指针:

struct threadImgs 
{   
  IMAGE * fuente;
  IMAGE * destino;
};

或者将线程函数设置为不拉指针:

void *processBMP(void *imgs)
{
  struct threadImgs * imagen = (struct threadImgs *) imgs;

  IMAGE imagefte = imagen->fuente;
  IMAGE imagedst = imagen->destino;

  ...