summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLasse Collin <lasse.collin@tukaani.org>2014-12-21 18:58:44 +0200
committerLasse Collin <lasse.collin@tukaani.org>2014-12-21 18:58:44 +0200
commitcbafa710918195dbba3db02c3fab4f0538235206 (patch)
tree6914d1b19a38c596bd9af2ef48107a9cda2e6a38
parent8dbb57238d372c7263cfeb3e7f7fd9a73173156a (diff)
downloadxz-cbafa710918195dbba3db02c3fab4f0538235206.tar.gz
Docs: Use lzma_cputhreads() in 04_compress_easy_mt.c.
-rw-r--r--doc/examples/04_compress_easy_mt.c30
1 files changed, 26 insertions, 4 deletions
diff --git a/doc/examples/04_compress_easy_mt.c b/doc/examples/04_compress_easy_mt.c
index 121d3b1..efe5697 100644
--- a/doc/examples/04_compress_easy_mt.c
+++ b/doc/examples/04_compress_easy_mt.c
@@ -31,10 +31,6 @@ init_encoder(lzma_stream *strm)
// No flags are needed.
.flags = 0,
- // Set the number of threads to use.
- // FIXME: Add how to autodetect a reasonable number.
- .threads = 4,
-
// Let liblzma determine a sane block size.
.block_size = 0,
@@ -57,6 +53,32 @@ init_encoder(lzma_stream *strm)
.check = LZMA_CHECK_CRC64,
};
+ // Detect how many threads the CPU supports.
+ mt.threads = lzma_cputhreads();
+
+ // If the number of CPU cores/threads cannot be detected,
+ // use one thread. Note that this isn't the same as the normal
+ // single-threaded mode as this will still split the data into
+ // blocks and use more RAM than the normal single-threaded mode.
+ // You may want to consider using lzma_easy_encoder() or
+ // lzma_stream_encoder() instead of lzma_stream_encoder_mt() if
+ // lzma_cputhreads() returns 0 or 1.
+ if (mt.threads == 0)
+ mt.threads = 1;
+
+ // If the number of CPU cores/threads exceeds threads_max,
+ // limit the number of threads to keep memory usage lower.
+ // The number 8 is arbitrarily chosen and may be too low or
+ // high depending on the compression preset and the computer
+ // being used.
+ //
+ // FIXME: A better way could be to check the amount of RAM
+ // (or available RAM) and use lzma_stream_encoder_mt_memusage()
+ // to determine if the number of threads should be reduced.
+ const uint32_t threads_max = 8;
+ if (mt.threads > threads_max)
+ mt.threads = threads_max;
+
// Initialize the threaded encoder.
lzma_ret ret = lzma_stream_encoder_mt(strm, &mt);