Is compression thread safe?

48 views
Skip to first unread message

Mike Versteeg

unread,
Jun 23, 2023, 11:53:56 AM6/23/23
to libjpeg-turbo User Discussion/Support
From what I've read I was led to believe the entire library is thread safe, but when I try to compress more than one image at the same time (each in its own thread) I get random memory errors. Are there certain requirements, other than creating a compression handle for each thread? Is it ok to create these in the main thread, or do they need to created & destroyed in the thread itself (which would create overhead)?

DRC

unread,
Jun 23, 2023, 12:24:33 PM6/23/23
to libjpeg-t...@googlegroups.com

It's thread safe as long as you use a separate TurboJPEG handle or jpeg_compress_struct instance and a separate JPEG destination buffer for each thread.  You cannot simultaneously compress to the same JPEG destination buffer using multiple threads.  The handles/instances and buffers can all be created in the main thread as long as the main thread doesn't try to use them concurrently with any of the worker threads.  If you want more help than that, then you're going to have to be more specific about the type of memory errors you're getting, your compute environment, and exactly what your code is trying to do.

On 6/23/23 11:53 AM, Mike Versteeg wrote:
From what I've read I was led to believe the entire library is thread safe, but when I try to compress more than one image at the same time (each in its own thread) I get random memory errors. Are there certain requirements, other than creating a compression handle for each thread? Is it ok to create these in the main thread, or do they need to created & destroyed in the thread itself (which would create overhead)? --
You received this message because you are subscribed to the Google Groups "libjpeg-turbo User Discussion/Support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to libjpeg-turbo-u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/libjpeg-turbo-users/948e1ef9-b9af-4e2d-bbcb-f0bf2c65a5e2n%40googlegroups.com.

Mike Versteeg

unread,
Jun 23, 2023, 1:33:19 PM6/23/23
to libjpeg-turbo User Discussion/Support
Thanks for confirming that Darrell! I must have made an error then.

BTW: so far love the library and its API, took me just one minute to get a working demo from only reading the API doc. That's impressive. Same goes for performance (though I always need more, hence the threads).

Good luck with the 3.0 release, looking forward to it!

DRC

unread,
Jun 23, 2023, 2:53:10 PM6/23/23
to libjpeg-t...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages