Enabling Multithreading
Multithreading must be enabled at compile time with theZSTD_MULTITHREAD build macro. When enabled, you can set the number of worker threads using compression parameters.
Basic Multithreading
Set the number of worker threads withZSTD_c_nbWorkers:
Set worker count
Choose the number of workers based on your CPU cores:When
nbWorkers >= 1, compression enters asynchronous mode.Complete Example
Fromexamples/streaming_compression.c with multithreading:
Thread Pool Sharing
When compressing multiple files in parallel, you can share a thread pool to avoid oversubscribing CPU cores.Using ZSTD_threadPool (Static Linking Only)
The thread pool API is only available when usingZSTD_STATIC_LINKING_ONLY:
Complete Thread Pool Example
Fromexamples/streaming_compression_thread_pool.c:
Multithreading Parameters
Fine-tune multithreaded compression with additional parameters:Job Size
Control the size of each compression job:ZSTDMT_JOBSIZE_MIN).
Overlap Size
Control how much data overlaps between jobs:- 0: Default (library determines based on strategy)
- 1: No overlap
- 6: w/8 overlap (common default)
- 9: Full window overlap
Command Line Usage
Use the-T flag to enable multithreading in the zstd CLI:
Performance Considerations
Memory Usage
Multithreading increases memory usage:- Each worker thread requires its own compression context
- Memory scales with:
nbWorkers × (windowSize + jobSize) - Higher compression levels use more memory per worker