Note that there are some explanatory texts on larger screens.

plurals
  1. POpthread condition variables vs win32 events (linux vs windows-ce)
    text
    copied!<p>I am doing a performance evaluation between Windows CE and Linux on an arm imx27 board. The code has already been written for CE and measures the time it takes to do different kernel calls like using OS primitives like mutex and semaphores, opening and closing files and networking.</p> <p>During my porting of this application to Linux (pthreads) I stumbled upon a problem which I cannot explain. Almost all tests showed a performance increase from 5 to 10 times but not my version of <a href="http://msdn.microsoft.com/en-us/library/aa450535.aspx" rel="nofollow">win32 events</a> (<a href="http://msdn.microsoft.com/en-us/library/ms886810.aspx" rel="nofollow"><code>SetEvent</code></a> and <a href="http://msdn.microsoft.com/en-us/library/aa450988.aspx" rel="nofollow"><code>WaitForSingleObject</code></a>), CE actually "won" this test.</p> <p>To emulate the behaviour I was using pthreads condition variables <em>(I know that my implementation doesn't fully emulate the CE version but it's enough for the evaluation)</em>.</p> <p>The test code uses two threads that "ping-pong" each other using events.</p> <hr> <h2>Windows code:</h2> <p><strong>Thread 1:</strong> (the thread I measure)</p> <pre><code>HANDLE hEvt1, hEvt2; hEvt1 = CreateEvent(NULL, FALSE, FALSE, TEXT("MyLocEvt1")); hEvt2 = CreateEvent(NULL, FALSE, FALSE, TEXT("MyLocEvt2")); ResetEvent(hEvt1); ResetEvent(hEvt2); for (i = 0; i &lt; 10000; i++) { SetEvent (hEvt1); WaitForSingleObject(hEvt2, INFINITE); } </code></pre> <p><strong>Thread 2:</strong> (just "responding")</p> <pre><code>while (1) { WaitForSingleObject(hEvt1, INFINITE); SetEvent(hEvt2); } </code></pre> <hr> <h2>Linux code:</h2> <p><strong>Thread 1:</strong> (the thread I measure)</p> <pre><code>struct event_flag *event1, *event2; event1 = eventflag_create(); event2 = eventflag_create(); for (i = 0; i &lt; 10000; i++) { eventflag_set(event1); eventflag_wait(event2); } </code></pre> <p><strong>Thread 2:</strong> (just "responding")</p> <pre><code>while (1) { eventflag_wait(event1); eventflag_set(event2); } </code></pre> <p>My implementation of <code>eventflag_*</code>:</p> <pre><code>struct event_flag* eventflag_create() { struct event_flag* ev; ev = (struct event_flag*) malloc(sizeof(struct event_flag)); pthread_mutex_init(&amp;ev-&gt;mutex, NULL); pthread_cond_init(&amp;ev-&gt;condition, NULL); ev-&gt;flag = 0; return ev; } void eventflag_wait(struct event_flag* ev) { pthread_mutex_lock(&amp;ev-&gt;mutex); while (!ev-&gt;flag) pthread_cond_wait(&amp;ev-&gt;condition, &amp;ev-&gt;mutex); ev-&gt;flag = 0; pthread_mutex_unlock(&amp;ev-&gt;mutex); } void eventflag_set(struct event_flag* ev) { pthread_mutex_lock(&amp;ev-&gt;mutex); ev-&gt;flag = 1; pthread_cond_signal(&amp;ev-&gt;condition); pthread_mutex_unlock(&amp;ev-&gt;mutex); } </code></pre> <p>And the <code>struct</code>:</p> <pre><code>struct event_flag { pthread_mutex_t mutex; pthread_cond_t condition; unsigned int flag; }; </code></pre> <h2>Questions:</h2> <ul> <li>Why doesn't I see the performance boost here?</li> <li>What can be done to improve performance (e.g are there faster ways to implement CEs behaviour)?</li> <li>I'm not used to coding pthreads, are there bugs in my implementation maybe resulting in performance loss?</li> <li>Are there any alternative libraries for this?</li> </ul>
 

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