Cilk plus Critical section and locks

Cilk plus Critical section and locks

Hi All,

I'm a grad student and I have difficulties in implementing critical section and locks in cilkplus. I used cilkplus on the latest gcc. Could someone plese put a simple pseudocode along with the header files that have to be used for critical section and locks, so that I can understnad & implement using my code. Thanks.


Sushek Shekar.

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

The limitation to be aware of is that there are no guarantees about which thread your code is working on at any spawn or sync boundary.  CRITICAL_SECTIONs on Windows an mutexes on Linux require the release to be on the same thread as the acquire.

The Cilkscreen tool should issue an error or warning if you're holding a lock at spawn or sync boundary.  You can download if from the Cilk Plus website at the page.

    - Barry


Barry Tannenbaum (Intel) wrote:
 ...mutexes on Linux require the release to be on the same thread as the acquire. 

Does that limitation hold for spinlocks or semaphores?  At the very least, you could implement your own spinlock using gcc's built-in compare-and-swap routine.  A semaphore would be a better choice if you want blocked threads to go to sleep.

It probably holds for semaphores, though of course you should check the OS documentation to be sure.

It won't hold for spinlocks, but be aware that the Cilk runtime has its own locks, and holding any kind of lock across a spawn or sync boundary runs the risk of lock order inversion which can lead to a deadlock.

    - Barry

Thanks for the reply.


cilk_for (int j=0; j<innerreps/nthreads; j++){



1. The function delay(delaylength) has to be implemented in the critical section. How do I use mutexes there?

2. I used #include<cilk_mutex.h> in the header, it says, no such file or directory exists. I did check going back to the insatllation files, there's a file called cilk_mutex.h. but #include<cilk.h> is working fine.


cilk_mutex.h was provided by cilk++, a prior implementation of Cilk that's no longer supported.  cilk_mutex provided a thin wrapper around a pthread_mutex_t.  We withdrew the feature because it's provided by other tools, like tbb and the new C++ standard.

You can use any locking mechanism supported by your OS.  Wikipedia has a reasonable introduction to critical sections: .  However, keep my warning about holding a lock across a spawn or sync boundary in mind.  You'll be safe as long as you acquire and release the lock to within the cilk_for loop body.

Note that you can frequently avoid using locks by using reducers, and reducers have some very nice properties that aren't possible with locks.  But that's probably the next topic to be covered, once you've got your locks working.  :o)

    - Barry

Thanks a lot for the reply. It helped me a lot. Will get back if I have any doubts/questions.


Sushek Shekar.

Leave a Comment

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