Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C编程-pthread_create()结构作为参数_C_Multithreading_Arguments_Structure_Lodepng - Fatal编程技术网

C编程-pthread_create()结构作为参数

C编程-pthread_create()结构作为参数,c,multithreading,arguments,structure,lodepng,C,Multithreading,Arguments,Structure,Lodepng,我一直在编写一个示例,演示如何在图像处理操作中使用POSIX进行多线程处理。编码是用lodepng库完成的。我希望代码将任何加载图像的颜色反转过程分解为4个相等的线程。尽管我已反复检查,但仍然无法找出为什么在threadFunc2()函数中修改第四个线程的起始值和结束值。这与其他3个线程获得正确的起始值和结束值所使用的代码相同。我使用了一个结构将开始、结束和线程数数据传递到函数中。例如,如果我加载一个10x10的图像,结果是100个像素(每个像素有4个R、G、B和透明度值),则需要一个400元素

我一直在编写一个示例,演示如何在图像处理操作中使用POSIX进行多线程处理。编码是用lodepng库完成的。我希望代码将任何加载图像的颜色反转过程分解为4个相等的线程。尽管我已反复检查,但仍然无法找出为什么在threadFunc2()函数中修改第四个线程的起始值和结束值。这与其他3个线程获得正确的起始值和结束值所使用的代码相同。我使用了一个结构将开始、结束和线程数数据传递到函数中。例如,如果我加载一个10x10的图像,结果是100个像素(每个像素有4个R、G、B和透明度值),则需要一个400元素的数组来存储每个颜色值。第四个线程应以300和399开头。它在传递给函数之前在main()函数中正确计算,但在开始时在threadFunc2()中分别为5和6

#include <pthread.h>
#include "lodepng.h"
#include <stdio.h>
#include <stdlib.h>
#include "lodepng.c"
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; 
unsigned int error;
unsigned int encError;
unsigned char* image;
unsigned int width;
unsigned int height;
int Arraysize;
const char* filename = "10x10Circle.png";
const char* newFileName = "10x10Result.png";    
struct Markers{int start;int end;int no};
void *threadFunc(void *arg){ // function to load the image into an array
    pthread_mutex_lock(&mutex);
    error = lodepng_decode32_file(&image, &width, &height, filename);
    if(error){
        printf("error %u: %s\n", error, lodepng_error_text(error));
    }
    Arraysize = 4*height*width;
    printf("arraysize:%d, %d \n",sizeof(image)/sizeof(image[0]),Arraysize);        
    pthread_mutex_unlock(&mutex);   
    return NULL;    }
void *threadFunc2(void *arg){ //function to apply inverse process on loaded data
   struct Markers*vars= (struct Markers*) arg;
   printf("Thread %d start|start-%d,end-%d\n",vars->no,vars->start,vars->end);
   for( int i = vars->start; i<(vars->end); i=i+4){
    pthread_mutex_lock(&mutex);
    image[0+i]= 255-image[0+i];
    image[1+i]= 255-image[1+i];
    image[2+i]= 255-image[2+i];
    image[3+i]= 255-image[3+i];//  can be ignored
    printf("Thread: %d,round:%d\n",vars->no,i);// debug line
    pthread_mutex_unlock(&mutex);
   }
  // printf("Thread: %d,after end:%d\n",vars->no,i);
     printf("**************************\n");
printf("Thread end\n");
return NULL;
}
void *encodeprocessed(){
    encError = lodepng_encode32_file(newFileName, image, width, height);
   if(encError){
    printf("error %u: %s\n", error, lodepng_error_text(encError));  }
   free(image);   }

代码正在运行,没有任何错误。图像仅反转75%并保存。非常感谢任何能给我提供线索的人。

如果要索引位置[3],则需要将其定义为结构标记位置[4]。每个
位置[3]
访问都会调用未定义的行为,因为它超出了数组的范围。关于:
#通常包括“lodepng.c”
,从不包含C源文件:
lodepng.C
的内容是什么?
lodepng.h
的内容是什么?数组中的有效索引是:0…(数组中的num元素-1)
int main(void){
  pthread_t pth,pth0, pth1, pth2, pth3;
  pthread_create(&pth,NULL,threadFunc,NULL);
  pthread_join(pth, NULL );
  struct Markers Positions[3];
  Positions[0].start = 0;
  Positions[0].end = Arraysize/4 -1;
  Positions[0].no = 1;
  Positions[1].start =Arraysize/4;
  Positions[1].end = Arraysize/2 -1;
  Positions[1].no = 2;
  Positions[2].start =Arraysize/2;
  Positions[2].end = Arraysize*3/4 -1;
  Positions[2].no = 3;
  Positions[3].start =(Arraysize*3)/4;
  Positions[3].end = Arraysize -1;
  Positions[3].no = 4; 
  //debug line
  printf("%d,%d,%d,%d,%d,%d,%d,%d,%d\n",Arraysize,Positions[0].start,Positions[0].end, 
  Positions[1].start,Positions[1].end,Positions[2].start,Positions[2].end,
  Positions[3].start,Positions[3].end);
  pthread_create(&pth0,NULL,threadFunc2,&Positions[0]);
  pthread_create(&pth1,NULL,threadFunc2,&Positions[1]);
  pthread_create(&pth2,NULL,threadFunc2,&Positions[2]);
  pthread_create(&pth3,NULL,threadFunc2,&Positions[3]);
  pthread_join(pth0, NULL );
  pthread_join(pth1, NULL );
  pthread_join(pth2, NULL );
  pthread_join(pth3, NULL );
  encodeprocessed();
  return 0;}