Using Cilk with Microsoft Visual C++

Using Cilk with Microsoft Visual C++


I am a MS Visual C++ developer and involved in writing server side network application development. Hear of Cilk has exited me. I googled and came to know that Cilk code can be compiled with MS Visual Studio IDE but I want to know a step futher. Can Cilk be used along with MFC classes? If yes, can you please direct me to the related references.

I am sorry if this is a very basic question. I am new to Cilk and want to know how best I can use it.

Thank you in advance.


6 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

Hi Chintan.

You will need to get a copy of Intel Parallel Studio 2011 or Intel Composer 2011. You can find links for these products (and a pointer to the Software Evaluation Center) at

Before you can start using Intel Cilk Plus features, you'll need to convert the project to an Intel C/C++ project. You can do this by right clicking on the project in the solution explorer and selecting the appropriate entry from the context menu.

Intel Parallel Studio and Intel Composer ship with sample Intel Cilk Plus code. The QuickDemo sample is a fully functional MFC application that runs a quicksort both serially and in parallel using Intel Cilk Plus, updating progress bars as they execute. this would be a good place to start.

The key thing to worry about with MFC applications is that you must consider the Cilk workers to be compute threads. They should never call MFC functions directly. Instead they should use PostMessage or SendMessage to transfer information to the UI thread.

Rules for MFC programmers:

  1. Only make MFC calls from the UI thread.
  2. Use Windows messages to pass information to the UI thread.
  3. The amount of information you can pass in a message is limited. Be VERY careful when you pass structures betweeen threads! You must be very clear about which thread creates the message, which thread destroys the message, and when

- Barry

Hi Barry,

Thank you very much for a detailed reply.

Would it be possible to use Windows IO Completion Port APIs like CreateIOCompletionPort(...), GetQueuedCompletionStatus(...) and Socket APIs while compiling application using Cilk?



Win32 API calls should be fine.

Be aware that when you wait on a IOCompletionPort or socket, you're
blocking the worker. Intel Cilk Plus programs are expected to have serial semantics,
which means you can run them just as well with 1 worker as with more.
This restriction can make them easier to debug, since all of your standard, serial tools work when you only use 1 worker; there's no stealing, so all of the stack traces are what you'd expect. But producer/consumer-style applications aren't a good fit.

The problem with MFC is that it stores state in thread local storage and there's no guarantee what thread you're running on in an Intel Cilk Plus application. Workstealing means that the code after a cilk_spawn can be stolen by any worker. The only way to make it work correctly is to marshall all of the UI display to the UI thread. Of course, that's the advice from Microsoft for ALL multi-threaded MFC applications; only the thread that creates a window should be accessing it.

- Barry

Yes, I understand the UI related issues.

But I trying to understand how Win32 APIs and Cilk functions can co-exist. You said Win32 API calls should be fine. Does this means that Cilk calls can be used in between Win32 API calls? OR Cilk code has to be in a completely separat file which will be compiled by Cilk modules and those classes/functions can be called from normal Visual C++ classes?

Using Cilk requires to install any run-time library just like .NET Framework?

Thanks & Regards,


The Intel Cilk Plus runtime is named cilkrts20.dll on Windows and shipped with the Intel C/C++ compiler in the /redist//compiler directory. I believe you have permission to redistribute the runtime which is why it's in a subdirectory of the "redist" directory. You must ensure that there's a copy of the runtime installed on the target system, and that it's in a directory that will be searched when the application (or your module) loads.

Intel Cilk Plus code can be used between Win32 API calls. You should be aware that waiting on a synchronization object of any type (Critical Section, Event, IOCompletionPort, socket, etc.) will stall the worker thread. This is unavoidable and not a problem, but you should know that the runtime makes no attempt to detect this or start up another system worker thread to compensate for it.

You should also be aware that work can be stolen at any strand boundary. A strand boundary is a cilk_spawn, cilk_sync or cilk_for statement. In general you should not hold any synchronization objects across a strand boundary. In particular, you must not hold a Critical Section across a strand boundary because you are required to call LeaveCriticalSection on the same thread that you called EnterCriticalSection on.

- Barry

Leave a Comment

Please sign in to add a comment. Not a member? Join today