Jump to content
c0d3r

Are TCompressionStream and TDecompressionStream thread safe?

Recommended Posts

HI, All

 

Using Delphi 10.4.2,  just wondering if TCompressionStream and TDecompressionStream in System.Zlib unit thread safe or not?  Thanks.

Share this post


Link to post
6 hours ago, c0d3r said:

Using Delphi 10.4.2,  just wondering if TCompressionStream and TDecompressionStream in System.Zlib unit thread safe or not?

Generally any kind of stream should be suitable for using in background threads, but also they cannot be simultaneously accessed and shared between threads. Just look at it logically - how can you use stream that holds a state (if nothing else than current position that changes as you read and write) be thread-safe? 

 

While you could in theory protect simultaneous access with locks and share stream across threads, you wouldn't gain much from that as stream would have to be locked and accessible to one thread at the time. If you think you could speed some operation by sharing stream and using is in parallel, you will only slow the whole process down, not speed it up.

 

When using TCompressionStream and TDecompressionStream in background thread, OpProgress event handler will run in the context of background thread and you need to take that into account if you want to show progress in the UI and synchronize code.

  • Like 3

Share this post


Link to post

Thanks,  I meant thread-personal instances, no shared streams.

Share this post


Link to post
12 hours ago, c0d3r said:

Thanks,  I meant thread-personal instances, no shared streams.

Yes, those two stream classes can be safely used in background threads if they are not shared.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×