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

  • SEARCH
  • Home
  • 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 7093557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:27:15+00:00 2026-05-28T08:27:15+00:00

In my application I have a number of objects which can perform some long

  • 0

In my application I have a number of objects which can perform some long lasting calculations, let’s call them clients. Also I have a number of objects which contain descriptions of tasks to be calculated. Something like this:

let clients = [1..4]
let tasks = [1..20]

let calculate c t =
    printf "Starting task %d with client %d\n" t c
    Thread.Sleep(3000)
    printf "Finished task %d with client %d\n" t c

With one client I can only start one task at a time.
I want to create a function/class which will assign the tasks to the clients and perform the calculations. I’ve done this in C# using a queue of clients, so as soon as a new task is assigned to a client, this client is removed from the queue and when the calculations are finished, the client is released and is placed in the queue again. Now I’m interested in implementing this in a functional way. I’ve tried to experiment with asynchronous workflows, but I cannot think of a proper way to implement this.

Here’s an F#-like code that I was trying to make work, but couldn’t:

let rec distribute clients tasks calculate tasks_to_wait =
    match clients, tasks with
    | _ , [] -> ()           // No tasks - we're done!
    | [], th::tt ->          // No free clients, but there are still tasks to calculate.
             let released_client = ?wait_for_one? tasks_to_wait 
             distribute [released_client] tasks calculate ?tasks_to_wait?

    | ch::ct, th::tt ->      // There are free clients. 
        let task_to_wait() =
             do calculate ch th 
             ch
        distribute ct tt calculate (task_to_wait::tasks_to_wait)

How do I do this? Is there a functional design pattern to solve this task?

  • 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-28T08:27:16+00:00Added an answer on May 28, 2026 at 8:27 am

    There are various ways to do this. It would be perfectly fine to use some concurrent collection (like a queue) from .NET 4.0 from F#, as this is often the easiest thing to do, if the collection already implements the functionality you need.

    The problem requires concurrent access to some resource, so it cannot be solved in a purely functional way, but F# provides agents, which give you a nice alternative way to solve the problem.

    The following snippet implements an agent that schedules the work items. It uses its own mailbox to keep the available clients (which gives you a nice way to wait for the next available client). After the agent is created, you can just send all the initial clients. It will continue iterating over the tasks while clients are available. When there is no available client, it will block (asynchronously – without blocking of threads), until some previous processing completes and a client is sent back to the agent’s mailbox:

    let workloadAgent = MailboxProcessor.Start(fun agent -> 
      // The body of the agent, runs as a recursive loop that iterates
      // over the tasks and waits for client before it starts processing
      let rec loop tasks = async {
        match tasks with 
        | [] -> 
            // No more work to schedule (but there may be still calculation,
            // which was started earlier, going on in the background)
            ()
        | task::tasks ->
            // Wait for the next client to become available by receiving the
            // next message from the inbox - the messages are clients
            let! client = agent.Receive()
            // Spanw processing of the task using the client
            async { calculate client task
                    // When the processing completes, send the client
                    // back to the agent, so that it can be reused
                    agent.Post(client) }
            |> Async.Start 
            // Continue processing the rest of the tasks
            return! loop tasks }
    
      // Start the agent with the initial list of tasks
      loop tasks )
    
    // Add all clients to the agent, so that it can start
    for client in clients do workloadAgent.Post(client)
    

    If you’re not familiar with F# agents, then the MSDN section Server-Side Programming has some useful information.

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

Sidebar

Related Questions

I have an application in which a number of objects are all extending an
In a window of my WPF application I have a number of objects which
I have an iPhone application in which a number of domain objects are populated
I have a number of icons used throughout an application - let's take ok/cancel
I have a number of enums in my application which are used as property
How can I reduce resources inside my application? I have tried a number of
I'm building an application which will have dynamic allocated objects of type A each
I'm attempting to test a Flex application in which Objects do not have static
I have a number of graphics objects which I am plotting in a graphics
I have an application which makes use of System.Timers.Timer objects to do expirations and

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.