Use this file to discover all available pages before exploring further.
Multithreaded compression allows Zstandard to use multiple CPU cores to compress data faster. This is particularly effective for large files and high compression levels.
Multithreading must be enabled at compile time with the ZSTD_MULTITHREAD build macro. When enabled, you can set the number of worker threads using compression parameters.
Set the number of worker threads with ZSTD_c_nbWorkers:
ZSTD_CCtx* const cctx = ZSTD_createCCtx();// Set compression levelCHECK_ZSTD(ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, cLevel));// Enable multithreading with N workersint nbThreads = 4;size_t const r = ZSTD_CCtx_setParameter(cctx, ZSTD_c_nbWorkers, nbThreads);if (ZSTD_isError(r)) { fprintf(stderr, "Note: the linked libzstd library doesn't support multithreading. " "Reverting to single-thread mode. \n");}
1
Check multithreading support
Test if the library supports multithreading:
// Try setting nbWorkers >= 1size_t result = ZSTD_CCtx_setParameter(cctx, ZSTD_c_nbWorkers, 1);if (ZSTD_isError(result)) { // Multithreading not supported fprintf(stderr, "Multithreading not available\n");} else { // Multithreading is supported printf("Using %d threads\n", nbThreads);}
2
Set worker count
Choose the number of workers based on your CPU cores:
// Use all available coresint nbThreads = sysconf(_SC_NPROCESSORS_ONLN);// Or set a specific numberint nbThreads = 4;ZSTD_CCtx_setParameter(cctx, ZSTD_c_nbWorkers, nbThreads);
When nbWorkers >= 1, compression enters asynchronous mode.
3
Compress as normal
Use streaming compression with ZSTD_compressStream2():
// Set job size (auto by default)ZSTD_CCtx_setParameter(cctx, ZSTD_c_jobSize, 1024 * 1024); // 1 MB jobs
Each job is compressed in parallel. Smaller jobs mean more parallelism but higher overhead. Default (0) automatically determines job size based on compression parameters.Minimum job size is 512 KB (ZSTDMT_JOBSIZE_MIN).