Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If CancellationToken is a struct and is passed by Value, how is it updated?

I see that CancellationToken is a struct https://docs.microsoft.com/en-us/dotnet/api/system.threading.cancellationtoken?view=netframework-4.7.1

If I pass a struct to a new function by value, it shouldn't be modified in the caller. So if i'm passing a CancellationTokenSource (by value), then I call cts.cancel(), how does the method that has a copy of that token is notified that it has been canceled? Shouldn't it work only if we pass by reference?

For example:

public static void Main()
{
    var cts = new CancellationTokenSource();
    SomeCancellableOperation(cts.Token);
    cts.cancel();
}

public void SomeCancellableOperation(CancellationToken token) {
...
    token.ThrowIfCancellationRequested();
...
}
like image 377
damule Avatar asked Jan 07 '18 07:01

damule


2 Answers

You can look at the source code for CancellationToken. The basic idea is that the token only stores a reference to a CancellationTokenSource.

internal CancellationToken(CancellationTokenSource source)
{
    m_source = source;
}

The CancellationTokenSource is what is modified when it is cancelled. Checking whether the token is cancelled really just goes back to the source:

public bool IsCancellationRequested 
{
    get
    {
        return m_source != null && m_source.IsCancellationRequested;
    }
}
like image 61
Mike Zboray Avatar answered Oct 11 '22 20:10

Mike Zboray


The struct is passed by value, but it contains a reference to a WaitHandle. The WaitHandle is global.

like image 29
John Wu Avatar answered Oct 11 '22 18:10

John Wu