Skip to content

Observation on "Generalized async return types" #7506

Closed

Description

@clperez

Hi. On the following example

public ValueTask<int> CachedFunc()
{
return (cache) ? new ValueTask<int>(cacheResult) : new ValueTask<int>(LoadCache());
}
private bool cache = false;
private int cacheResult;
private async Task<int> LoadCache()
{
// simulate async work:
await Task.Delay(100);
cacheResult = 100;
cache = true;
return cacheResult;
}

Shouldn't cache and cacheResult variable be accesses through some kind of barrier like Volatile or interlocked?

They are being written and read from different threads... right?

Regards.


Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Activity

  1. added this to the Backlog milestone on Sep 13, 2018
  2. BillWagner commented on Sep 13, 2018

    @BillWagner
    Member

    Thanks for writing this @clperez

    The sample could be structured better to ensure that LoadCache is only called once. I've added this to our backlog to be addressed as part of updating the async articles

  3. self-assigned this
    on Mar 24, 2019
  4. modified the milestones: Backlog, March 2019 on Mar 24, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions