Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 820241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T02:26:37+00:00 2026-05-15T02:26:37+00:00

I’m working on a thread-safe collection that uses Dictionary as a backing store. In

  • 0

I’m working on a thread-safe collection that uses Dictionary as a backing store.

In C# you can do the following:

  private IEnumerable<KeyValuePair<K, V>> Enumerate() {
     if (_synchronize) {
        lock (_locker) {
           foreach (var entry in _dict)
              yield return entry;
        }
     } else {
        foreach (var entry in _dict)
           yield return entry;
     }
  }

The only way I’ve found to do this in F# is using Monitor, e.g.:

    let enumerate() =
        if synchronize then
            seq {
                System.Threading.Monitor.Enter(locker)
                try for entry in dict -> entry
                finally System.Threading.Monitor.Exit(locker)
            }
        else seq { for entry in dict -> entry }

Can this be done using the lock function? Or, is there a better way to do this in general? I don’t think returning a copy of the collection for iteration will work because I need absolute synchronization.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T02:26:38+00:00Added an answer on May 15, 2026 at 2:26 am

    I don’t think that you’ll be able to do the same thing with the lock function, since you would be trying to yield from within it. Having said that, this looks like a dangerous approach in either language, since it means that the lock can be held for an arbitrary amount of time (e.g. if one thread calls Enumerate() but doesn’t enumerate all the way through the resulting IEnumerable<_>, then the lock will continue to be held).

    It may make more sense to invert the logic, providing an iter method along the lines of:

    let iter f =
      if synchronize then
        lock locker (fun () -> Seq.iter f dict)
      else
        Seq.iter f dict
    

    This brings the iteration back under your control, ensuring that the sequence is fully iterated (assuming that f doesn’t block, which seems like a necessary assumption in any case) and that the lock is released immediately thereafter.

    EDIT

    Here’s an example of code that could hold the lock forever.

    let cached = enumerate() |> Seq.cache
    let firstFive = Seq.take 5 cached |> Seq.toList
    

    We’ve taken the lock in order to start enumerating through the first 5 items. However, we haven’t continued through the rest of the sequence, so the lock won’t be released (maybe we would enumerate the rest of the way later based on user feedback or something, in which case the lock would finally be released).

    In most cases, correctly written code will ensure that it disposes of the original enumerator, but there’s no way to guarantee that in general. Therefore, your sequence expressions should be designed to be robust to only being enumerated part way. If you intend to require your callers to enumerate the collection all at once, then forcing them to pass you the function to apply to each element is better than returning a sequence which they can enumerate as they please.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.