c0d3r 17 Posted March 23, 2023 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
Fr0sT.Brutal 900 Posted March 23, 2023 What exact safety you mean? One instance across threads or thread-personal instances? Share this post Link to post
Dalija Prasnikar 1396 Posted March 23, 2023 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. 3 Share this post Link to post
c0d3r 17 Posted March 23, 2023 Thanks, I meant thread-personal instances, no shared streams. Share this post Link to post
Dalija Prasnikar 1396 Posted March 24, 2023 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