Page 1 of 1

[SOLVED] Imagick multithread problem

Posted: 2012-10-22T13:04:40-07:00
by m4nti5
Hi all!,

I've been implementing an aplication that uses Imagick, and testing with multiple threads I found a problem with the MagickReadImageBlob function. I'm using Fedora 14, I used yum to install ImageMagick-devel-6.6.4.1-15.fc14.i686, the kernell release is 2.6.35.11-83.fc14.i686.PAE. The problem can be reproduced with:

Code: Select all

#include <stdio.h>
#include <pthread.h>
#include <wand/MagickWand.h>

unsigned char *get_file_blob(char *file_path,size_t *length){
	FILE *fd;
	unsigned char *blob;
	size_t sz;
	fd = fopen(file_path,"r");
	if(!fd)
		return NULL;
	fseek(fd,0L,SEEK_END);
	sz = ftell(fd);
	fclose(fd);
	fd = fopen(file_path,"r");
	if(length)
		*length = sz;
	blob = malloc(sz);
	fread(blob,sz,1,fd);
	fclose(fd);
	return blob;
}
pthread_mutex_t img_lock;

void *thread(void *arg){
	size_t length;
	unsigned char *blob;
	MagickWand *magick_wand;
	char *path = (char *)arg;
	//pthread_mutex_lock(&img_lock);
	magick_wand = NewMagickWand();
	blob = get_file_blob(path,&length);
        if(!blob){
                DestroyMagickWand(magick_wand);
                //pthread_mutex_unlock(&img_lock);
                return NULL;
        }
	MagickBooleanType status = MagickReadImageBlob(magick_wand, blob, length);
	DestroyMagickWand(magick_wand);
	//pthread_mutex_unlock(&img_lock);
	free(blob);
	pthread_detach(pthread_self());
	return NULL;
}

int main(int argc, char *argv[]){
	if(argc != 2)
		return 1;
	MagickWandGenesis();
	int i;
	pthread_mutex_init(&img_lock,NULL);
	for(i = 0; i < 20; ++i){
		pthread_t thread_id;
		pthread_create(&thread_id,NULL,thread,argv[1]);
	}
	pthread_mutex_destroy(&img_lock);
	MagickWandTerminus();
	return 0;
}
I compile with:

Code: Select all

gcc -g -o test test.c `Wand-config --cflags` `Wand-config --libs` -lpthread
If I uncomment the pthread_mutex_lock and pthread_mutex_unlock calls, everything works perfect. Otherwise most of the time generates a core with the message:

Code: Select all

tiff_problem: magick/hashmap.c:1975: ResetLinkedListIterator: Assertion `list_info != (LinkedListInfo *) ((void *)0)' failed.
Am I doing something wrong???,

Thanks in advance

Re: Imagick multithread problem

Posted: 2012-10-22T17:51:50-07:00
by magick
Logic error. You call MagickWandTerminus() before your threads are complete. You need to call pthread_join().

Re: Imagick multithread problem

Posted: 2012-10-26T12:52:13-07:00
by m4nti5
Thanks for your response, you're right, the code I provided had a thread ending issue.