Loading images asynchronously
Posted: 2014-03-07T06:05:24-07:00
I'm using the MagickWand C API (6.8.7-7-Q16-x86-dll) and I encounter errors from time to time when loading multiple images concurrently. Consider the following piece of code:
This code is not fully runnable but should give you an idea about how to trigger this error. It simply starts two threads which load two images concurrently. Sometimes, loading one of the images fails. The error message returned by MagickGetException then is:
no decode delegate for this image format `' @ error/blob.c/BlobToImage/358
Unfortunately, this error is hard to trigger. Often it occurs only on the first run of the application, but runs fine when executing it again. What I realized is, that this error only happens when the first images are loaded, so I could imagine that the error is due to some kind of global library initialization. Do I need to call a function for that? I tried to call MagickWandGenesis at the beginning of main, but that didn't fix it.
Thank you for any ideas about what I'm doing wrong. For now I'm using a workaround by avoiding to load images concurrently.
Code: Select all
void loadImage(const std::string& filename) {
std::ifstream file(filename,std::ifstream::binary);
std::ostringstream blob;
blob << file.rdbuf();
MagickWand* wand = NewMagickWand();
MagickReadImageBlob(wand,blob.str().data(),blob.str().size());
ExceptionType type;
const char* msg = MagickGetException(wand,&type);
if (type != UndefinedException) {
std::cout << "error: " << msg << std::endl;
} else {
std::cout << "successful" << std::endl;
}
DestroyMagickWand(wand);
}
void runner(boost::asio::io_service& service) {
service.run();
}
int main() {
boost::asio::io_service service;
boost::asio::io_service::work work(service);
service.post(boost::bind(&loadImage,std::string("test1.jpg")));
service.post(boost::bind(&loadImage,std::string("test2.jpg")));
boost::thread t1(boost::bind(&runner,boost::ref(service)));
boost::thread t2(boost::bind(&runner,boost::ref(service)));
std::cin.ignore();
return EXIT_SUCCESS;
}
no decode delegate for this image format `' @ error/blob.c/BlobToImage/358
Unfortunately, this error is hard to trigger. Often it occurs only on the first run of the application, but runs fine when executing it again. What I realized is, that this error only happens when the first images are loaded, so I could imagine that the error is due to some kind of global library initialization. Do I need to call a function for that? I tried to call MagickWandGenesis at the beginning of main, but that didn't fix it.
Thank you for any ideas about what I'm doing wrong. For now I'm using a workaround by avoiding to load images concurrently.