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 7519727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:57:02+00:00 2026-05-30T01:57:02+00:00

I’m in OCaml. I’m looking to simulate communicating nodes to look at how quickly

  • 0

I’m in OCaml.

I’m looking to simulate communicating nodes to look at how quickly messages propagate under different communication schemes etc.

The nodes can 1. send and 2. receive a fixed message. I guess the obvious thing to do is have each node as a separate thread.

Apparently you can get threads to pass messages to each other using the Event module and channels, but I can’t find any examples of this. Can someone point me in the right direction or just give me a simple relevant example?

Thanks a lot.

  • 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-30T01:57:04+00:00Added an answer on May 30, 2026 at 1:57 am

    If you are going to attempt a simulation, then you will need a lot more control over your nodes than simply using threads will allow — or at least, without major pains.

    My subjective approach to the topic would be to create a simple, single-threaded virtual machine in order to keep full control over the simulation. The easiest way to do so in OCaml is to use a monad-like structure (as is done in Lwt, for instance) :

    (* A thread is a piece of code that can be executed to perform some
       side-effects and fork zero, one or more threads before returning. 
       Some threads may block when waiting for an event to happen. *)
    type thread = < run : thread list ; block : bool >
    
    (* References can be used as communication channels out-of-the box (simply 
       read and write values ot them). To implement a blocking communication 
       pattern, use these two primitives: *)
    
    let write r x next = object (self) 
      method block = !r <> None
      method run   = if self # block then [self]
                     else r := Some x ; [next ()]
    end
    
    let read r next = object (self) 
      method block = !r = None
      method run   = match r with 
                      | None -> [self]
                      | Some x -> r := None ; [next x]
    end
    

    You can create better primitives that suit your needs, such as adding a “time required for transmitting” property in your channels.

    The next step is defining a simulation engine.

    (* The simulation engine can be implemented as a simple queue. It starts 
       with a pre-defined set of threads and returns when no threads are left, 
       or when all threads are blocking. *)
    let simulate threads = 
      let q = Queue.create () in 
      let () = List.iter (fun t -> Queue.push t q) threads in 
      let rec loop blocking = 
        if Queue.is_empty q then `AllThreadsTerminated else 
          if Queue.length q = blocking then `AllThreadsBlocked else 
            let thread = Queue.pop q in 
            if thread # block then ( 
              Queue.push thread q ; 
              loop (blocking + 1) 
            ) else ( 
              List.iter (fun t -> Queue.push t q) (thread # run) ; 
              loop 0
            ) 
      in
      loop 0 
    

    Again, you may adjust the engine to keep track of what node is executing which thread, to keep per-node priorities in order to simulate one node being massively slower or faster than others, or randomly picking a thread for execution on every step, and so on.

    The last step is executing a simulation. Here, I’m going to have two threads sending random numbers back and forth.

    let rec thread name input output = 
      write output (Random.int 1024) (fun () -> 
        read input (fun value ->
          Printf.printf "%s : %d" name value ; 
          print_newline () ;
          thread name input output
      ))
    
    let a = ref None and b = ref None 
    let _ = simulate [ thread "A -> B" a b ; thread "B -> A" b a ]        
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is

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.