In my previous posts, I addressed the motivation behind using concurrency runtime’s synchronization primitives and also introduced Critical Section and reader writer lock. In this blog, I will cover concurrency runtime’s event.
This is a bi-state type class which, unlike Critical Section or Reader Writer Lock, does not protect access to shared data. Events synchronize flow of execution and use concurrency runtime’s facilities to enable cooperative schedule of work. They behave similar to Win32 manual-reset event. The main difference between the concurrency runtime’s event and Win32 event is that the concurrency runtime’s event are designed to cooperatively yield to other cooperative tasks in the runtime when blocked in addition to preempting whereas Win32 events are, by design purely pre-emptive in nature.
This example enables the scheduler to create two threads and then calls DemoEvent function that takes event class and a Win32 manual-reset event class as template parameters. The Demo function first creates several tasks that simulate some work and then wait for a shared event to become signaled.
Special thanks to Rick Molloy for sharing this example.
// event.cpp : Defines the entry point for the console application.
// compile with: /EHsc
using namespace Concurrency;
using namespace std;
void wait(int count = INFINITE)
LONG volatile taskCtr = 0;
//create a taskgroup and schedule multiple copies of the task
for(int i = 0;i < 8; ++i)
//Simulate some work
printf_s("\tTask %d waiting for the event\n", InterlockedIncrement(&taskCtr));
printf_s(" Setting the event\n");
//Set the event
//wait for the tasks
void main ()
// Create a scheduler that uses two threads.
CurrentScheduler::Create(SchedulerPolicy(2, MinConcurrency, 2, MaxConcurrency, 2));
Task 1 waiting for the event
Task 2 waiting for the event
Task 3 waiting for the event
Task 4 waiting for the event
Task 5 waiting for the event
Setting the event
We observe that when using cooperative event, we execute all 5 tasks, each task when waiting on the event that is not set, cooperatively yields so that another task can be scheduled and run in the thread’s quantum. When using windows event, we observe that the 2 tasks scheduled block the thread until the event is set.