Posted By


keigoi on 09/13/09

Tagged


Statistics


Viewed 420 times
Favorited by 1 user(s)

Related snippets


pthread sleeper fairness


/ Published in: C
Save to your folder(s)

<p>this snippet is for checking fairness of context switching in your environment.</p><p>the snippet does not runs two threads directly, but it switches two threads explicitly, by using a monitor (a mutex and a condition variable).</p><p>Some programming languges take such indirect concurrent execution mechanism for native threads. Programming languges which support \"native threads\" but does not support conccurent GC must stop the whole process in order to collect garbages safely. Since native threads generally are not capable of disabling preemption, the runtime must pause all threads except for one that runs GC. </p><p>compile with:<code>gcc -lpthread filename.c</code></p><p>there is a `global lock\' and two threads. each thread runs work() in the same way, only except its parameter. each of two threads increments the counter hit_count[i] where i is the id of the thread.</p><p>after 1 second, the program outputs statistics and terminates.</p><p><code>SIGALRM. thread0: 1899, thread1: 2333, switch:20</code></p><p>if your pthread library is fair, the first two numbers are nearly equal to each other, and the third number is 20 or so.</p><p>unfortunately, my Mac OS X (10.5.8) has turned to be unfair:</p><p><code>SIGALRM. thread0: 4086, thread1: 215, switch:2</code></p><p>on the contrary, linux (debian 5.0, kenel 2.6.26) has shown to be fair. please post your results in the comments.</p><p>The linux kernel 2.6 (>= 2.6.23) is likely to be fair, because it is equipped with the <a href=\"http://en.wikipedia.org/wiki/Completely_Fair_Scheduler\">Completely Fair Scheduler</a>. This scheduler is aware of \'sleeper fairness\'.</p><p><strong>see also:</strong><a href=\"http://snipplr.com/view/19509/\">http://snipplr.com/view/19509/</a> if your pthread is shown to be \'unfair\', this O\'Caml snippet will also stops for a while.</p><p><strong>UPDATE: </strong> added <code>volatile</code> to the shared variables.</p>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.