{"id":99,"date":"2020-02-03T09:47:34","date_gmt":"2020-02-03T07:47:34","guid":{"rendered":"http:\/\/www.juustila.com\/antti\/?p=99"},"modified":"2020-02-06T10:03:07","modified_gmt":"2020-02-06T08:03:07","slug":"to-thread-or-not-to-thread","status":"publish","type":"post","link":"https:\/\/www.juustila.com\/antti\/2020\/02\/03\/to-thread-or-not-to-thread\/","title":{"rendered":"To thread or not to thread"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">There&#8217;s a distributed C++ system I made, used as a &#8220;patient&#8221; in a course on Software architectures. It includes a command line tool <em>TestDataGenerator<\/em>, which I implemented to test the performance and reliability of the system. The tool generates random data in memory buffers and then writes four test data files which are read and handled by the system&#8217;s distributed nodes. <a href=\"http:\/\/www.juustila.com\/antti\/2019\/12\/13\/generating-test-data-with-c\/\">An earlier blog post<\/a> discussed the tool&#8217;s implementation details.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The generator was single threaded, writing the four data files in sequence, in the main thread. But then this stupid idea popped in my head &#8212; what if the four test data files are written to disk in parallel? Would it be faster? How much if any? <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Threading is absolutely&nbsp;<em>not<\/em>&nbsp;needed in this case: generating test data for 5000 students takes about 250ms using my MacBook Pro (13-inch, 2018), 2.3 GHz four core Intel Core i5, 1 Tb SSD disk. On machines with HDDs this could be somewhat slower.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, I wanted to see how much of execution time (if any) I can squeeze off with the four threads, each writing to their own data file from the RAM buffers. Also an opportunity to learn more about threads. Those horrible, evil things everyone is saying nobody should use&#8230;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">My first implementation where the threads were&nbsp;<em>created<\/em>&nbsp;and executed when the memory buffer was full, and saving the file done in a lambda function:<\/p>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #111111; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\"><pre style=\"margin: 0; line-height: 125%\"> <span style=\"color: #fb660a; font-weight: bold\">if<\/span> <span style=\"color: #ffffff\">(bufferCounter<\/span> <span style=\"color: #ffffff\">&gt;=<\/span> <span style=\"color: #ffffff\">bufSize)<\/span> <span style=\"color: #ffffff\">{<\/span>\n   <span style=\"color: #ffffff\">std::<\/span><span style=\"color: #fb660a; font-weight: bold\">thread<\/span> <span style=\"color: #ffffff\">thread1(<\/span> <span style=\"color: #ffffff\">[&amp;isFirstWrite,<\/span> <span style=\"color: #ffffff\">&amp;STUDENT_BASIC_INFO_FILE,<\/span> <span style=\"color: #ffffff\">&amp;basicInfoBuffer]<\/span> <span style=\"color: #ffffff\">{<\/span>\n     <span style=\"color: #ffffff\">saveBuffer(isFirstWrite,<\/span> <span style=\"color: #ffffff\">STUDENT_BASIC_INFO_FILE,<\/span> <span style=\"color: #ffffff\">basicInfoBuffer);<\/span>\n   <span style=\"color: #ffffff\">});<\/span>\n<span style=\"color: #008800; font-style: italic; background-color: #0f140f\">\/\/ ...<\/span>\n<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">But creating a thread&nbsp;<em>takes time<\/em>. Lots of time, thousands of processor cycles, depending on your setup (see e.g. this&nbsp;<a href=\"https:\/\/lemire.me\/blog\/2020\/01\/30\/cost-of-a-thread-in-c-under-linux\/\">blog post<\/a>). If the tool startup parameters are <code>-s 50000 -b 500<\/code> (create 50000 records with buffer size of 500), this would mean 50000\/500 = 100 thread creations per file, so 400 threads would be created during the execution of the tool. Not very good for performance.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I changed the implementation to create the four threads&nbsp;<em>only once<\/em>, before filling and saving the memory buffers:<\/p>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #111111; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\"><pre style=\"margin: 0; line-height: 125%\">   <span style=\"color: #008800; font-style: italic; background-color: #0f140f\">\/\/ For coordination between main thread and writer threads<\/span>\n   <span style=\"color: #ffffff\">std::atomic&lt;<\/span><span style=\"color: #cdcaa9; font-weight: bold\">int<\/span><span style=\"color: #ffffff\">&gt;<\/span> <span style=\"color: #ffffff\">threadsFinished{<\/span><span style=\"color: #0086f7; font-weight: bold\">0<\/span><span style=\"color: #ffffff\">};<\/span>\n   <span style=\"color: #008800; font-style: italic; background-color: #0f140f\">\/\/ Prepare four threads that save the data.<\/span>\n   <span style=\"color: #ffffff\">std::vector&lt;std::<\/span><span style=\"color: #fb660a; font-weight: bold\">thread<\/span><span style=\"color: #ffffff\">&gt;<\/span> <span style=\"color: #ffffff\">savers;<\/span>\n   <span style=\"color: #ffffff\">savers.push_back(std::<\/span><span style=\"color: #fb660a; font-weight: bold\">thread<\/span><span style=\"color: #ffffff\">(&amp;threadFuncSavingData,<\/span> <span style=\"color: #ffffff\">std::ref(threadsFinished),<\/span> <span style=\"color: #ffffff\">std::cref(STUDENT_BASIC_INFO_FILE),<\/span> <span style=\"color: #ffffff\">std::ref(basicInfoBuffer)));<\/span>\n   <span style=\"color: #ffffff\">savers.push_back(std::<\/span><span style=\"color: #fb660a; font-weight: bold\">thread<\/span><span style=\"color: #ffffff\">(&amp;threadFuncSavingData,<\/span> <span style=\"color: #ffffff\">std::ref(threadsFinished),<\/span> <span style=\"color: #ffffff\">std::cref(EXAM_INFO_FILE),<\/span> <span style=\"color: #ffffff\">std::ref(examInfoBuffer)));<\/span>\n   <span style=\"color: #008800; font-style: italic; background-color: #0f140f\">\/\/ ... and same for the remaining two threads.<\/span>\n<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">and then woken up every time the data buffers were full:<\/p>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #111111; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\"><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #fb660a; font-weight: bold\">if<\/span> <span style=\"color: #ffffff\">(bufferCounter<\/span> <span style=\"color: #ffffff\">&gt;=<\/span> <span style=\"color: #ffffff\">bufSize)<\/span> <span style=\"color: #ffffff\">{<\/span>\n   <span style=\"color: #fb660a; font-weight: bold\">if<\/span> <span style=\"color: #ffffff\">(verbose)<\/span> <span style=\"color: #ffffff\">std::cout<\/span> <span style=\"color: #ffffff\">&lt;&lt;<\/span> <span style=\"color: #ffffff\">std::endl<\/span> <span style=\"color: #ffffff\">&lt;&lt;<\/span> <span style=\"color: #0086d2\">\"Activating buffer writing threads...\"<\/span> <span style=\"color: #ffffff\">&lt;&lt;<\/span> <span style=\"color: #ffffff\">std::endl;<\/span>\n   <span style=\"color: #008800; font-style: italic; background-color: #0f140f\">\/\/ Prepare variables for the file saving threads.<\/span>\n   <span style=\"color: #ffffff\">startWriting<\/span> <span style=\"color: #ffffff\">=<\/span> <span style=\"color: #ffffff\">true;<\/span>\n   <span style=\"color: #ffffff\">threadsFinished<\/span> <span style=\"color: #ffffff\">=<\/span> <span style=\"color: #0086f7; font-weight: bold\">0<\/span><span style=\"color: #ffffff\">;<\/span>\n   <span style=\"color: #cdcaa9; font-weight: bold\">int<\/span> <span style=\"color: #ffffff\">currentlyFinished<\/span> <span style=\"color: #ffffff\">=<\/span> <span style=\"color: #0086f7; font-weight: bold\">0<\/span><span style=\"color: #ffffff\">;<\/span>\n   <span style=\"color: #008800; font-style: italic; background-color: #0f140f\">\/\/ And launch the file writing threads.<\/span>\n   <span style=\"color: #ffffff\">launchWrite.notify_all();<\/span>\n<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">And then the main thread waits for the writers to finish their job before filling the memory buffers again.<\/p>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #111111; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\"><pre style=\"margin: 0; line-height: 125%\">   <span style=\"color: #008800; font-style: italic; background-color: #0f140f\">\/\/ Wait for the writer threads to finish.<\/span>\n   <span style=\"color: #fb660a; font-weight: bold\">while<\/span> <span style=\"color: #ffffff\">(threadsFinished<\/span> <span style=\"color: #ffffff\">&lt;<\/span> <span style=\"color: #0086f7; font-weight: bold\">4<\/span><span style=\"color: #ffffff\">)<\/span> <span style=\"color: #ffffff\">{<\/span>\n      <span style=\"color: #ffffff\">std::unique_lock&lt;std::mutex&gt;<\/span> <span style=\"color: #ffffff\">ulock(fillBufferMutex);<\/span>\n      <span style=\"color: #ffffff\">writeFinished.wait(ulock,<\/span> <span style=\"color: #ffffff\">[&amp;]<\/span> <span style=\"color: #ffffff\">{<\/span>\n         <span style=\"color: #fb660a; font-weight: bold\">return<\/span> <span style=\"color: #ffffff\">currentlyFinished<\/span> <span style=\"color: #ffffff\">!=<\/span> <span style=\"color: #ffffff\">threadsFinished;<\/span>\n      <span style=\"color: #ffffff\">});<\/span>\n      <span style=\"color: #ffffff\">currentlyFinished<\/span> <span style=\"color: #ffffff\">=<\/span> <span style=\"color: #ffffff\">threadsFinished;<\/span>\n   <span style=\"color: #ffffff\">}<\/span>\n<span style=\"color: #ffffff\">\ufeff<\/span>\n<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Obviously the file writing threads notify the main thread about them finishing the file operations using a condition variable and a counter the main thread can use to keep track of if all the writer threads finished:<\/p>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #111111; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\"><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #008800; font-style: italic; background-color: #0f140f\">\/\/ Thread function saving data in parallel when notified that buffers are full.<\/span>\n<span style=\"color: #cdcaa9; font-weight: bold\">void<\/span> <span style=\"color: #ff0086; font-weight: bold\">threadFuncSavingData<\/span><span style=\"color: #ffffff\">(std::atomic&lt;<\/span><span style=\"color: #cdcaa9; font-weight: bold\">int<\/span><span style=\"color: #ffffff\">&gt;<\/span> <span style=\"color: #ffffff\">&amp;<\/span> <span style=\"color: #ffffff\">finishCount,<\/span> <span style=\"color: #fb660a; font-weight: bold\">const<\/span> <span style=\"color: #ffffff\">std::string<\/span> <span style=\"color: #ffffff\">&amp;<\/span> <span style=\"color: #ffffff\">fileName,<\/span> <span style=\"color: #ffffff\">std::vector&lt;std::string&gt;<\/span> <span style=\"color: #ffffff\">&amp;<\/span> <span style=\"color: #ffffff\">buffer)<\/span> <span style=\"color: #ffffff\">{<\/span>\n   <span style=\"color: #cdcaa9; font-weight: bold\">bool<\/span> <span style=\"color: #ffffff\">firstRound<\/span> <span style=\"color: #ffffff\">=<\/span> <span style=\"color: #ffffff\">true;<\/span>\n   <span style=\"color: #fb660a; font-weight: bold\">while<\/span> <span style=\"color: #ffffff\">(running)<\/span> <span style=\"color: #ffffff\">{<\/span>\n      <span style=\"color: #008800; font-style: italic; background-color: #0f140f\">\/\/ Wait for the main thread to notify the buffers are ready to be written to disk.<\/span>\n      <span style=\"color: #ffffff\">std::unique_lock&lt;std::mutex&gt;<\/span> <span style=\"color: #ffffff\">ulock(writeMutex);<\/span>\n      <span style=\"color: #ffffff\">launchWrite.wait(ulock,<\/span> <span style=\"color: #ffffff\">[&amp;]<\/span> <span style=\"color: #ffffff\">{<\/span>\n         <span style=\"color: #fb660a; font-weight: bold\">return<\/span> <span style=\"color: #ffffff\">startWriting<\/span> <span style=\"color: #ffffff\">||<\/span> <span style=\"color: #ffffff\">!running;<\/span>\n      <span style=\"color: #ffffff\">});<\/span>\n      <span style=\"color: #008800; font-style: italic; background-color: #0f140f\">\/\/ We are still running and writing, so do it.<\/span>\n      <span style=\"color: #fb660a; font-weight: bold\">if<\/span> <span style=\"color: #ffffff\">(buffer.size()<\/span> <span style=\"color: #ffffff\">&gt;<\/span> <span style=\"color: #0086f7; font-weight: bold\">0<\/span> <span style=\"color: #ffffff\">&amp;&amp;<\/span> <span style=\"color: #ffffff\">startWriting<\/span> <span style=\"color: #ffffff\">&amp;&amp;<\/span> <span style=\"color: #ffffff\">running)<\/span> <span style=\"color: #ffffff\">{<\/span>\n         <span style=\"color: #ffffff\">saveBuffer(firstRound,<\/span> <span style=\"color: #ffffff\">fileName,<\/span> <span style=\"color: #ffffff\">buffer);<\/span>\n         <span style=\"color: #ffffff\">buffer.clear();<\/span>\n         <span style=\"color: #ffffff\">firstRound<\/span> <span style=\"color: #ffffff\">=<\/span> <span style=\"color: #ffffff\">false;<\/span>\n         <span style=\"color: #008800; font-style: italic; background-color: #0f140f\">\/\/ Update the counter that this thread is now ready.<\/span>\n         <span style=\"color: #008800; font-style: italic; background-color: #0f140f\">\/\/ Main thread waits that four threads have finished (count is 4).<\/span>\n         <span style=\"color: #ffffff\">finishCount++;<\/span>\n      <span style=\"color: #ffffff\">}<\/span>\n      <span style=\"color: #008800; font-style: italic; background-color: #0f140f\">\/\/ Notify the main thread.<\/span>\n      <span style=\"color: #ffffff\">writeFinished.notify_one();<\/span>\n   <span style=\"color: #ffffff\">}<\/span>\n<span style=\"color: #ffffff\">}<\/span>\n<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Then to measurements. I created a script which executes the tool 20 times, first using threads and then sequentially; not using threads (command line parameter <code>-z<\/code> disables the threading code and uses sequential code):<\/p>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #f0f3f3; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\"><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #336666\">echo<\/span> <span style=\"color: #CC3300\">\"Run this in the build directory of TestDataGenerator.\"<\/span>\n<span style=\"color: #336666\">echo<\/span> <span style=\"color: #CC3300\">\"Removing output files...\"<\/span>\nrm <span style=\"color: #336666\">test<\/span>-*.txt\n<span style=\"color: #336666\">echo<\/span> <span style=\"color: #CC3300\">\"Running threaded tests...\"<\/span>\n<span style=\"color: #006699; font-weight: bold\">for<\/span> <span style=\"color: #555555\">((<\/span><span style=\"color: #003333\">i<\/span> <span style=\"color: #555555\">=<\/span> 0; i &lt; 20; i++<span style=\"color: #555555\">))<\/span>; <span style=\"color: #006699; font-weight: bold\">do<\/span> .\/GenerateTestData -s 50000 -e 10 -b 500 &gt;&gt; <span style=\"color: #336666\">test<\/span>-par.txt; <span style=\"color: #006699; font-weight: bold\">done<\/span>\n<span style=\"color: #336666\">echo<\/span> <span style=\"color: #CC3300\">\"Running sequential tests...\"<\/span>\n<span style=\"color: #006699; font-weight: bold\">for<\/span> <span style=\"color: #555555\">((<\/span><span style=\"color: #003333\">i<\/span> <span style=\"color: #555555\">=<\/span> 0; i &lt; 20; i++<span style=\"color: #555555\">))<\/span>; <span style=\"color: #006699; font-weight: bold\">do<\/span> .\/GenerateTestData -zs 50000 -e 10 -b 500 &gt;&gt; <span style=\"color: #336666\">test<\/span>-seq.txt; <span style=\"color: #006699; font-weight: bold\">done<\/span>\n<span style=\"color: #336666\">echo<\/span> <span style=\"color: #CC3300\">\"-- Tests done -- \"<\/span>\nopen <span style=\"color: #336666\">test<\/span>-*.txt\n<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Just to compare, I executed the tests in two machines. MacBook Pro 2.3 GHz Intel Core i5 with four cores, 1 Tb SSD and iMac 2015 with HDD. Next, I took the output files and from there the amount of milliseconds the tool took each time, to a Numbers file and generated these graphics from the test data:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2020\/02\/N\u00e4ytt\u00f6kuva-2020-2-5-kello-18.37.08.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"388\" src=\"https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2020\/02\/N\u00e4ytt\u00f6kuva-2020-2-5-kello-18.37.08-1024x388.png\" alt=\"Comparison of sequential and threaded execution in two machines.\" class=\"wp-image-108\" srcset=\"https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2020\/02\/N\u00e4ytt\u00f6kuva-2020-2-5-kello-18.37.08-1024x388.png 1024w, https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2020\/02\/N\u00e4ytt\u00f6kuva-2020-2-5-kello-18.37.08-300x114.png 300w, https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2020\/02\/N\u00e4ytt\u00f6kuva-2020-2-5-kello-18.37.08-768x291.png 768w, https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2020\/02\/N\u00e4ytt\u00f6kuva-2020-2-5-kello-18.37.08-1536x582.png 1536w, https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2020\/02\/N\u00e4ytt\u00f6kuva-2020-2-5-kello-18.37.08-2048x776.png 2048w, https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2020\/02\/N\u00e4ytt\u00f6kuva-2020-2-5-kello-18.37.08-1568x594.png 1568w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/a><figcaption>Comparison of sequential and threaded execution in two machines<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">As you can see, there is no difference in writing in threads (parallel) or writing sequentially. Here you can see how the threads take turns and execute in parallel in the cores of the processor of the MacBook Pro:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2020\/02\/N\u00e4ytt\u00f6kuva-2020-2-5-kello-12.33.06.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"613\" src=\"https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2020\/02\/N\u00e4ytt\u00f6kuva-2020-2-5-kello-12.33.06-1024x613.png\" alt=\"Profiler showing threads executing.\" class=\"wp-image-112\" srcset=\"https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2020\/02\/N\u00e4ytt\u00f6kuva-2020-2-5-kello-12.33.06-1024x613.png 1024w, https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2020\/02\/N\u00e4ytt\u00f6kuva-2020-2-5-kello-12.33.06-300x179.png 300w, https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2020\/02\/N\u00e4ytt\u00f6kuva-2020-2-5-kello-12.33.06-768x459.png 768w, https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2020\/02\/N\u00e4ytt\u00f6kuva-2020-2-5-kello-12.33.06-1536x919.png 1536w, https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2020\/02\/N\u00e4ytt\u00f6kuva-2020-2-5-kello-12.33.06-2048x1225.png 2048w, https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2020\/02\/N\u00e4ytt\u00f6kuva-2020-2-5-kello-12.33.06-1568x938.png 1568w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/a><figcaption>Blue areas show when the threads are active, executing.<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Profiling the execution shows that having multiple threads doing the work won&#8217;t make a difference. In the trace below you can see that most the time the threads are either waiting for their turn to flush the data to disk or actually flushing the data. Most of the time in the selected saveBuffer method is spent in flushing data.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2020\/02\/N\u00e4ytt\u00f6kuva-2020-2-5-kello-18.45.56.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"563\" src=\"https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2020\/02\/N\u00e4ytt\u00f6kuva-2020-2-5-kello-18.45.56-1024x563.png\" alt=\"Profiler screenshot shows where time was spent, flushing and waiting.\" class=\"wp-image-109\" srcset=\"https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2020\/02\/N\u00e4ytt\u00f6kuva-2020-2-5-kello-18.45.56-1024x563.png 1024w, https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2020\/02\/N\u00e4ytt\u00f6kuva-2020-2-5-kello-18.45.56-300x165.png 300w, https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2020\/02\/N\u00e4ytt\u00f6kuva-2020-2-5-kello-18.45.56-768x423.png 768w, https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2020\/02\/N\u00e4ytt\u00f6kuva-2020-2-5-kello-18.45.56-1536x845.png 1536w, https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2020\/02\/N\u00e4ytt\u00f6kuva-2020-2-5-kello-18.45.56-2048x1127.png 2048w, https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2020\/02\/N\u00e4ytt\u00f6kuva-2020-2-5-kello-18.45.56-1568x863.png 1568w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/a><figcaption>Selected lines show where the most of the time was spent.<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Also, in the sequential execution, where the single main thread does all, time is spend in flushing to disk:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2020\/02\/N\u00e4ytt\u00f6kuva-2020-2-5-kello-18.53.10.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"566\" src=\"https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2020\/02\/N\u00e4ytt\u00f6kuva-2020-2-5-kello-18.53.10-1024x566.png\" alt=\"Single threaded execution profile.\" class=\"wp-image-110\" srcset=\"https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2020\/02\/N\u00e4ytt\u00f6kuva-2020-2-5-kello-18.53.10-1024x566.png 1024w, https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2020\/02\/N\u00e4ytt\u00f6kuva-2020-2-5-kello-18.53.10-300x166.png 300w, https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2020\/02\/N\u00e4ytt\u00f6kuva-2020-2-5-kello-18.53.10-768x425.png 768w, https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2020\/02\/N\u00e4ytt\u00f6kuva-2020-2-5-kello-18.53.10-1536x850.png 1536w, https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2020\/02\/N\u00e4ytt\u00f6kuva-2020-2-5-kello-18.53.10-2048x1133.png 2048w, https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2020\/02\/N\u00e4ytt\u00f6kuva-2020-2-5-kello-18.53.10-1568x867.png 1568w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/a><figcaption>Single threaded execution spent most of the time flushing data to disk.<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Creating threads to speed up writing to disk &#8212; definitely not a good idea in this case. If this would be an app with GUI, then writing large amounts of data in a thread <em>could<\/em> very well be a good idea. If writing would take more than a couple of hundred milliseconds, user would notice the GUI lagging\/not being responsive. So whether to use threads or not to write data to disk, depends on your use case.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This <a href=\"https:\/\/www.drdobbs.com\/parallel\/multithreaded-file-io\/220300055?pgno=3\">oldish article from DrDobbs<\/a> is also an interesting read. Writing several files in threads is not necessarily helpful (unless using RAID), and that one should make threading configurable (like the <code>-z<\/code> parameter in my implementation) because they may in some situations even slow down the app. Also this <a href=\"https:\/\/softwareengineering.stackexchange.com\/questions\/385856\/design-of-file-i-o-processing-file-i-o-system\">discussion<\/a> on when to apply threads is a good one:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>Using multiple threads is most helpful when your program is CPU bound and you want to\u00a0parallelise your program to use multiple CPU cores.<\/p><p>This is <em>not the case for I\/O bound problems<\/em>, such as your scenario. Multiple threads will likely not speed up your system at all.<\/p><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>There&#8217;s a distributed C++ system I made, used as a &#8220;patient&#8221; in a course on Software architectures. It includes a command line tool TestDataGenerator, which I implemented to test the performance and reliability of the system. The tool generates random data in memory buffers and then writes four test data files which are read and &hellip; <a href=\"https:\/\/www.juustila.com\/antti\/2020\/02\/03\/to-thread-or-not-to-thread\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;To thread or not to thread&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_hide_featured_image":false,"_jetpack_featured_image_duplicate_dismissed":0,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[2],"tags":[10,39,41,8,17,40],"class_list":["post-99","post","type-post","status-publish","format-standard","hentry","category-coding","tag-c","tag-performance","tag-profiling","tag-stdthread","tag-threads","tag-writing-to-disk"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/posts\/99","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/comments?post=99"}],"version-history":[{"count":14,"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/posts\/99\/revisions"}],"predecessor-version":[{"id":120,"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/posts\/99\/revisions\/120"}],"wp:attachment":[{"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/media?parent=99"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/categories?post=99"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/tags?post=99"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}