Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Well, in fact all these explainations are congruent.</p> <p>The most simplified (and hence not 100% accurate) explaination of an event is to see an event as kind of a flag service provided by the operating system. A signaled Event can be seen as a set flag, an unsignalled event on the other hand can be seen as an unset flag.</p> <p>For implementing a producer/consumer thread-system based on flags, you usually do something like the following (note for the sake of simplicity i neglect further synchronization mechanisms):</p> <pre><code>static volatile int flag = 0; static volatile char data = 'A'; // Some code to initialize the threads void producer() { while (1) { Sleep(1000); data++; flag = 1; } } void consumer() { while (1) { /* Busy wait for the occurence of more data */ while (!flag) { // wait for next data } flag = 0; // process data } } </code></pre> <p>Unluckily this would lead to a waste of processor cycles in the busy wait loop or unwanted deferral of execution due to a <code>Sleep</code> call introduced to lower the CPU consumption. Both is unwanted.</p> <p>In order to avoid such problems with task synchronization, operating systems provide different flag like mechanisms (e.g. Events in Windows). With events, setting and resetting a flag is done by the OS calls <code>SetEvent</code>/<code>ResetEvent</code>. To check for a flag you can use <code>WaitForSingleObject</code>. This call has the power to put a task to sleep until the event is signalled which is optimal in terms of CPU consumption. </p> <p>This turns the above example into something like this:</p> <pre><code>static volatile char data = 'A'; static HANDLE newDataEvent = INVALID_HANDLE_VALUE; // Some code to initialize the threads and the newDataEvent handle void producer() { while (1) { Sleep(1000); data++; SetEvent(newDataEvent); } } void consumer() { while (1) { if (WaitForSingleObject(newDataEvent, INFINITE) == WAIT_OBJECT_0) { ResetEvent(newDataEvent); // process data } } } </code></pre>
 

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