[SOLVED] Imagick multithread problem

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
Post Reply
m4nti5
Posts: 2
Joined: 2012-10-22T12:46:16-07:00
Authentication code: 67789

[SOLVED] Imagick multithread problem

Post 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
Last edited by m4nti5 on 2012-10-26T12:55:16-07:00, edited 1 time in total.
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Imagick multithread problem

Post by magick »

Logic error. You call MagickWandTerminus() before your threads are complete. You need to call pthread_join().
m4nti5
Posts: 2
Joined: 2012-10-22T12:46:16-07:00
Authentication code: 67789

Re: Imagick multithread problem

Post by m4nti5 »

Thanks for your response, you're right, the code I provided had a thread ending issue.
Post Reply