Hi all, first allow me to apologize if there is an answer to my question already in the forums... I did some searching, but didnt find anything that related directly to my question.
I am currently playing around with the pthread library and attempted a very simple example. The test consists of adding a single value to a large array of pixels/an image (large being 10 000x5 000 indecies).
My current implementation (below) consists of two methods: one for performing the addition operation and one for managing the threads. The addition function takes a struct containing the arguments: a pointer to an image/array-containing-struct and two vertical boundaries. The addition function then operates on the paramaterized image within the given boudaries.
The second function creates two threads: one for each half of the image/array.
While the task is completed without error, there is NO speed increase in comparison to a non-threaded addition function.
Here is the code I have for the two functions (I can post the other source and header files if someone would like to compile the example).
Code:
Image * add5Threaded(Image * image)
{
addArgs arg1;
arg1.height0 = 0;
arg1.height1 = image->height/2;
arg1.image = image;
addArgs arg2;
arg2.height0 = image->height/2;
arg2.height1 = image->height;
arg2.image = image;
pthread_t tid1;
pthread_t tid2;
int ret;
ret = pthread_create(&tid1, NULL, add5_algorithm, &arg1);
ret = pthread_create(&tid2, NULL, add5_algorithm, &arg2);
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
return image;
}
void * add5_algorithm(addArgs * args)
{
Pixel pixel;
int x, y;
for(y = args->height0; y < args->height1; y++) {
for(x = 0; x < args->image->width; x++) {
pixel = getPixel(args->image, x, y);
pixel.r += 100;
pixel.b += 100;
pixel.g += 100;
//pthread_mutex_lock(&image_mutex);
setPixel(args->image, x, y, pixel);
fixPixel(args->image, x, y);
//pthread_mutex_unlock(&image_mutex);
}
}
pthread_exit(NULL);
}
for reference, the arguments:
Code:
typedef struct struct_addArgs {
int height0;
int height1;
Image * image;
}addArgs;
Also, when I use mutexs, the code takes FOREVER. Im guessing this is the deadlock problem I read about?
Anyway, any help is greatly appreciated!!
thanks