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

风格不统一 提交于 2019-12-05 03:41:41

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;
    }
}

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!