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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:15:52+00:00 2026-06-05T16:15:52+00:00

I haven’t used multithreading in Clojure at all so am unsure where to start.

  • 0

I haven’t used multithreading in Clojure at all so am unsure where to start.

I have a doseq whose body can run in parallel. What I’d like is for there always to be 3 threads running (leaving 1 core free) that evaluate the body in parallel until the range is exhausted. There’s no shared state, nothing complicated – the equivalent of Python’s multiprocessing would be just fine.

So something like:

(dopar 3 [i (range 100)]
  ; repeated 100 times in 3 parallel threads...
  ...)

Where should I start looking? Is there a command for this? A standard package? A good reference?

So far I have found pmap, and could use that (how do I restrict to 3 at a time? looks like it uses 32 at a time – no, source says 2 + number of processors), but it seems like this is a basic primitive that should already exist somewhere.

clarification: I really would like to control the number of threads. I have processes that are long-running and use a fair amount of memory, so creating a large number and hoping things work out OK isn’t a good approach (example which uses a significant chunk available mem).

update: Starting to write a macro that does this, and I need a semaphore (or a mutex, or an atom i can wait on). Do semaphores exist in Clojure? Or should I use a ThreadPoolExecutor? It seems odd to have to pull so much in from Java – I thought parallel programming in Clojure was supposed to be easy… Maybe I am thinking about this completely the wrong way? Hmmm. Agents?

  • 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-06-05T16:15:53+00:00Added an answer on June 5, 2026 at 4:15 pm

    pmap will actually work fine in most circumstances – it uses a thread pool with a sensible number of threads for your machine. I wouldn’t bother trying to create your own mechanisms to control the number of threads unless you have real benchmark evidence that the defaults are causing a problem.

    Having said that, if you really want to limit to a maximum of three threads, an easy approach is to just use pmap on 3 subsets of the range:

    (defn split-equally [num coll] 
      "Split a collection into a vector of (as close as possible) equally sized parts"
      (loop [num num 
             parts []
             coll coll
             c (count coll)]
        (if (<= num 0)
          parts
          (let [t (quot (+ c num -1) num)]
            (recur (dec num) (conj parts (take t coll)) (drop t coll) (- c t)))))) 
    
    (defmacro dopar [thread-count [sym coll] & body]
     `(doall (pmap 
        (fn [vals#]
          (doseq [~sym vals#]
            ~@body))  
        (split-equally ~thread-count ~coll))))
    

    Note the use of doall, which is needed to force evaluation of the pmap (which is lazy).

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

Sidebar

Related Questions

I haven't seen this question anywhere else, I hope someone can help. I have
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Haven't seen anything, but I have seen that you can create albums in the
haven't used regex replaces much and am not sure if how I have done
I haven't used the STL much before, but I started to on this huffman
I haven't used ARC yet other than to deal with it when it forces
I haven't been able to get this working and all of the sample code
I have a jquery bug and I've been looking for hours now, I can't
I haven't worked with SQL Reporting much, however I have been trying to get
Haven't been programming in JS for a while. Now, I have following thing: <html>

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.