Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You need to synchronize access to <code>validated</code> if it is shared variable. The easies way is to use <code>InterlockedIncrement</code> function instead of standard <code>++</code> operator when you want to increment it. Another approach is to use some kind of synchronization object like spinlock or mutex when you access the shared variable, but that is overkill if you just need to synchronize increment operation.</p> <p>If you want more details, please provide code of <code>collatz</code> function.</p> <p>As 'usr' suggested, for the better performance you can use separate variable fore each thread and then sum them in the main thread. In this scenario, you should pad those variables in such a way so they don't share same cache-line to avoid <a href="http://en.wikipedia.org/wiki/False_sharing" rel="nofollow">false sharing</a>.</p> <p>You haven't provided <code>collatz_thread</code> function, which could be another cause of inconsistent results. The reason is you pass <em>pointer</em> to variable (<code>&amp;thread</code>) that stores thread # which changes between the calls that create new threads, so depending on the state of the OS's scheduler the new threads might not get a chance to start while <code>thread</code> variable is already changed to have another value, so then you'll have more then one thread doing the same set of data, while other sets might be skipped. Since the behavior depends on current state of thread scheduler it's pretty much unpredictable.</p> <p>The solution is cast <code>thread</code> variable to <code>void*</code> instead of passing its address and then in <code>collatz_thread</code> function cast it back to <code>int</code>:</p> <p><code>HANDLE h1 = (HANDLE)_beginthreadex(NULL, 0, collatz_thread, (void*)thread, 0, NULL);</code></p> <p>And as Martin suggested, you possibly have integer overflow, but it shouldn't cause inconsistent results, just wrong results, but consistent nevertheless. </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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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