Note that there are some explanatory texts on larger screens.

plurals
  1. POSimple Multi-threading help? C++, WaitForSingleObject and Synchronization
    primarykey
    data
    text
    <p>So, I'm trying to make a simple multi-threaded program that validates the Collatz Conjecture for a large set of numbers and returns the total amount of validated numbers. Each thread (total 4) does an interval of numbers and updates the "validated" variable when a number reaches 1. I'm also timing the entire process (to compare vs a single threaded computation)</p> <p>The problem I'm having is that there is never any consistency when I print out the "validated" int at the end of the program, so I am guessing that either the threads are writing over each other, or the main thread is completing before the others, thus printing an incomplete number. I'm also assuming that the clock() calculations will be off too, if the main thread is completing before the others. So, how do I STOP the main thread from continuing until the other threads are completed (thus making it wait for a updated validated count and complete an accurate time measurement)? This is what I thought WaitForSingleObject did, but I'm guessing it just stops the main thread from EXITING, still allowing it to compute its other functions.</p> <p>This is my first go at any multi-threading, and I don't think I quite understand the workings of synchronization and the WaitForSingleObject command. Here is what I have so far in my main function:</p> <p><em><strong>EDIT: Here is my updated Main function and Collatz function. I modified it so that each thread is accessing a separate variable to avoid the synchronization issue, but the problem still persists. There is no consistent value when I print out "validated" *</em></strong></p> <p><em><strong>EDIT AGAIN: Alright, so I removed the "thread" int per Mladen Janković, and just used a simple counter to distribute the different intervals to the created threads. There is now a consistent, correct value for "validated". HOWEVER, I still cannot get the program to actually finish when there are 1,000,000 numbers. Testing it for 100,000 or even 10,000 works flawlessly, but when I up it to 1,000,000 numbers, the program runs indefinitely (hours) without actually returning a values. I'm guessing that it is getting stuck at a particular value (eg 750831 as Martin James pointed out). I tried substituting int for long int, but it seems that it still suffers from overflow. Any suggestions? And thanks for the immense help.</em></strong> </p> <p><em><strong>LAST EDIT: Alright, so I just used long long instead of int and now the program works flawlessly. Thanks for the help everyone!</em></strong></p> <pre><code>void main() { clock_t start; clock_t finish; unsigned int thread = 0; start = clock(); HANDLE h1 = (HANDLE)_beginthreadex(NULL, 0, collatz_thread, NULL, 0, NULL); HANDLE h2 = (HANDLE)_beginthreadex(NULL, 0, collatz_thread, NULL, 0, NULL); HANDLE h3 = (HANDLE)_beginthreadex(NULL, 0, collatz_thread, NULL, 0, NULL); for (int i = 750001 ; i &lt;= 1000000 ; i++) { collatz(i, 4); } WaitForSingleObject( h1, INFINITE ); WaitForSingleObject( h2, INFINITE ); WaitForSingleObject( h3, INFINITE ); finish = clock() - start; double time = finish / (double) CLOCKS_PER_SEC; validated = v1 + v2 + v3 + v4; cout &lt;&lt; validated &lt;&lt; " numbers validated." &lt;&lt; endl; cout &lt;&lt; endl &lt;&lt; time &lt;&lt; " seconds." &lt;&lt; endl; } unsigned _stdcall collatz_thread (void* n) { selection++; // selects a different interval each time collatz_thread is called switch (selection) { case 1: for (int i = 1 ; i &lt;= 250000; i++) { collatz(i, 1); } break; case 2: for (int i = 250001 ; i &lt;= 500000; i++) { collatz(i, 2); } break; case 3: for (int i = 500001 ; i &lt;= 750000; i++) { collatz(i, 3); } break; } return 0; } int collatz (int n, int thread) { int original = n; while (n != 1) { if (n%2 == 0) n = (n/2); else n = (3*n + 1); } if (n == 1) { switch (thread) { case 1: v1++; break; case 2: v2++; break; case 3: v3++; break; case 4: v4++; break; } return n; } </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload