How to a synchronize tasks?

user380719 picture user380719 · Apr 12, 2012 · Viewed 17.7k times · Source

Say I have an async method which saves to file:

async Task SaveToFileAsync()
{
   var file = await folder.GetFileAsync ( ...)
   var stream = file.OpenFileAsync(...)
   ///etc

}

Now imagine that SaveToFileAsync is called twice simultaneously. This is a problem because you can't write on the same file simultaneously

If this were a regular method, lock() would fix this:

void SaveToFile()
{
   lock(something)
   {
      /// code here
   }
}

However, lock is not allowed in an async method.

Of course, one could call Monitor.Enter() or use a mutex but these objects work with threads, not tasks. Therefore they are not the answer.

So since lock() is not an option, how can synchronize multiple tasks? In particular, what code should I write to ensure that "SaveToFileAsync" is called only once at a time?

Answer

dtb picture dtb · Apr 12, 2012

For an async mutual exclusion mechanism, have a look at

    Building Async Coordination Primitives, Part 6: AsyncLock

You could use the AsyncLock class follows:

private readonly AsyncLock m_lock = new AsyncLock();

async Task SaveToFileAsync()
{
    using (await m_lock.LockAsync()) 
    { 
        var file = await folder.GetFileAsync(...);
        var stream = await file.OpenFileAsync(...);
        // etc
    }
}