# How to spawn a variable number of tasks on startup for SPARK?

**URL:** https://forum.ada-lang.io/t/how-to-spawn-a-variable-number-of-tasks-on-startup-for-spark/4694
**Category:** General
**Tags:** ada, spark
**Created:** [August 30, 2026, 5:27am UTC](https://forum.ada-lang.io/t/how-to-spawn-a-variable-number-of-tasks-on-startup-for-spark/4694 "2026-08-30T05:27:30Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![ValorZard](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/valorzard/32/623_2.png) [@ValorZard](https://forum.ada-lang.io/u/ValorZard)
#### Post date: [August 30, 2026, 5:27am UTC](https://forum.ada-lang.io/t/how-to-spawn-a-variable-number-of-tasks-on-startup-for-spark/4694/1 "2026-08-30T05:27:30Z")

</div>

Hello!

Messing around a bit more with Ada, but a problem I"m running into is spawning a variable number of tasks on startup (specifically aiming for a “thread per core”).

For whatever reason, I can’t seem to figure out a way to get this working with SPARK.

Is there some way to do this? Maybe passing in the CPU core amount through a CLI argument or something?

```ada
package Workers is

   Worker_Count : constant Positive := (I want Task Per CPU here)

   task type Worker;

   type Worker_Array is array (Positive range <>) of Worker;

  Workers : Worker_Array (1 .. Worker_Count);

end Workers; 

```

---

<div class="post-metadata">

### Author: ![damaki](https://forum.ada-lang.io/letter_avatar_proxy/v4/letter/d/3bc359/32.png) [@damaki](https://forum.ada-lang.io/u/damaki)
#### Post date: [August 30, 2026, 8:54am UTC](https://forum.ada-lang.io/t/how-to-spawn-a-variable-number-of-tasks-on-startup-for-spark/4694/2 "2026-08-30T08:54:36Z")

</div>

I think the function `Number_Of_CPUs` in package `System.Multiprocessors` is what you’re after here.

---

<div class="post-metadata">

### Author: ![Irvise](https://forum.ada-lang.io/letter_avatar_proxy/v4/letter/i/7ba0ec/32.png) [@Irvise](https://forum.ada-lang.io/u/Irvise)
#### Post date: [August 30, 2026, 12:04pm UTC](https://forum.ada-lang.io/t/how-to-spawn-a-variable-number-of-tasks-on-startup-for-spark/4694/3 "2026-08-30T12:04:14Z")

</div>

AFAIK, SPARK can only work with tasks if they follow the Ravenscar and Jorvik profiles. Both profiles require tasks to be pinned to a CPU (no dynamic dispatch) and they cannot die. I do not know how you would be able to create an unbounded set of tasks on a core where you do not know how many tasks you have and how many cores you may run on…

You can read more about this on [Concurrency and Real-Time - learn.adacore.com](https://learn.adacore.com/courses/Ada_For_The_Embedded_C_Developer/chapters/03_Concurrency.html) and [Use the Jorvik Profile (CON02) - learn.adacore.com](https://learn.adacore.com/courses/Guidelines_for_Safe_and_Secure_Ada_SPARK/chapters/guidelines/concurrency/con02_use_the_jorvik_profile.html)

Maybe have a compile-time evaluation of some formula/function that will allow the compiler to optimize the number out and not make it a runtime thing?

Best regards,  
Fer

---

<div class="post-metadata">

### Author: ![OneWingedShark](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/onewingedshark/32/305_2.png) [@OneWingedShark](https://forum.ada-lang.io/u/OneWingedShark)
#### Post date: [August 30, 2026, 1:44pm UTC](https://forum.ada-lang.io/t/how-to-spawn-a-variable-number-of-tasks-on-startup-for-spark/4694/4 "2026-08-30T13:44:46Z")

</div>

```ada
Worker_Count : constant Positive := System.Multiprocessors.Number_Of_CPUs;

```

---

<div class="post-metadata">

### Author: ![ValorZard](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/valorzard/32/623_2.png) [@ValorZard](https://forum.ada-lang.io/u/ValorZard)
#### Post date: [August 30, 2026, 3:46pm UTC](https://forum.ada-lang.io/t/how-to-spawn-a-variable-number-of-tasks-on-startup-for-spark/4694/5 "2026-08-30T15:46:49Z")

</div>

So I tried this but System.Multiprocessors does not work in spark

@damaki @OneWingedShark

---

<div class="post-metadata">

### Author: ![ValorZard](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/valorzard/32/623_2.png) [@ValorZard](https://forum.ada-lang.io/u/ValorZard)
#### Post date: [August 30, 2026, 3:48pm UTC](https://forum.ada-lang.io/t/how-to-spawn-a-variable-number-of-tasks-on-startup-for-spark/4694/6 "2026-08-30T15:48:15Z")

</div>

Yeah, maybe if there was some way to see the number at compile time with a .env file or something? Not totally sure if that would work with SPARK

---

<div class="post-metadata">

### Author: ![ValorZard](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/valorzard/32/623_2.png) [@ValorZard](https://forum.ada-lang.io/u/ValorZard)
#### Post date: [August 30, 2026, 4:04pm UTC](https://forum.ada-lang.io/t/how-to-spawn-a-variable-number-of-tasks-on-startup-for-spark/4694/7 "2026-08-30T16:04:42Z")

</div>

Okay I think the best shot here is to take these two [files](https://github.com/gcc-mirror/gcc/blob/9c40d80365f334e01f6e864e62ca9965841774cb/gcc/ada/libgnat/s-multip.adb) And I can either vendor them into my own project and slap a SPARK\_Mode on them, or send a patch to GCC to add spark\_mode on them.

> <https://github.com/gcc-mirror/gcc/blob/9c40d80365f334e01f6e864e62ca9965841774cb/gcc/ada/libgnat/s-multip.ads>

> <https://github.com/gcc-mirror/gcc/blob/9c40d80365f334e01f6e864e62ca9965841774cb/gcc/ada/libgnat/s-multip.adb>

---

<div class="post-metadata">

### Author: ![Irvise](https://forum.ada-lang.io/letter_avatar_proxy/v4/letter/i/7ba0ec/32.png) [@Irvise](https://forum.ada-lang.io/u/Irvise)
#### Post date: [August 30, 2026, 4:06pm UTC](https://forum.ada-lang.io/t/how-to-spawn-a-variable-number-of-tasks-on-startup-for-spark/4694/8 "2026-08-30T16:06:52Z")

</div>

> [@ValorZard](#):
>
> Yeah, maybe if there was some way to see the number at compile time with a .env file or something? Not totally sure if that would work with SPARK

This is not specific to SPARK. You can use GPRbuild to read external variables and inject those variables into the source code. I do not know if it uses specific GNAT features (preprocessor).

Alire for example generates an .ads file at build time that can be used by the application. That file takes external information and makes it available to your Ada application. You could take this other approach with something like Makefiles for example.

I hope this helps. Best regards,  
Fer

---

<div class="post-metadata">

### Author: ![damaki](https://forum.ada-lang.io/letter_avatar_proxy/v4/letter/d/3bc359/32.png) [@damaki](https://forum.ada-lang.io/u/damaki)
#### Post date: [August 30, 2026, 4:13pm UTC](https://forum.ada-lang.io/t/how-to-spawn-a-variable-number-of-tasks-on-startup-for-spark/4694/9 "2026-08-30T16:13:07Z")

</div>

Ah, looks like this file is not yet in SPARK. Instead of vendoring or trying to patch the runtime, I recommend instead wrapping this function whose spec is in SPARK but the body is hidden.

In your package spec (with `SPARK_Mode => On`):

```ada
function Number_Of_CPUs return System.Multiprocessor.CPU
with Global => null;

```

Then in your package body, you can hide the implementation from SPARK:

```ada
function Number_Of_CPUs return System.Multiprocessor.CPU
with SPARK_Mode => Off
is
begin
   return System.Multiprocessor.Number_Of_CPUs;
end Number_Of_CPUs;

```

---

<div class="post-metadata">

### Author: ![sbenitezb](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/sbenitezb/32/285_2.png) [@sbenitezb](https://forum.ada-lang.io/u/sbenitezb)
#### Post date: [August 30, 2026, 4:43pm UTC](https://forum.ada-lang.io/t/how-to-spawn-a-variable-number-of-tasks-on-startup-for-spark/4694/10 "2026-08-30T16:43:07Z")

</div>

Make a wrapper for System.Multiprocessors.Number\_Of\_CPUs, disable SPARK in the body.

---

<div class="post-metadata">

### Author: ![ValorZard](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/valorzard/32/623_2.png) [@ValorZard](https://forum.ada-lang.io/u/ValorZard)
#### Post date: [August 30, 2026, 6:09pm UTC](https://forum.ada-lang.io/t/how-to-spawn-a-variable-number-of-tasks-on-startup-for-spark/4694/11 "2026-08-30T18:09:37Z")

</div>

Okay so that ended up working but now I have a bit of an awkward workaround.

Essentially, since I’d like for this to work on computers with differing levels of CPU cores, I have a Max Amount of Workers, and only a small subset of those get pinned to a CPU core, and the rest have to share a single leftover CPU core.

this is needed cuz Jorvik doesn’t allow for dynamic spawning of Tasks, so the best way around this is to just spawn an array of tasks and spawn more than I actually need to cover my bases here.

This state of affairs sucks especially if for example I was running this bit of code on something that has only 4 cores, which mean one core would have to run 30 tasks.

Is there any way to work around this or is this just how the cookie crumbles?

I figure if I’m stuck with this design decision, I could just make that leftover CPU core used for “low priority jobs” that don’t really need the fast thread pinning route.
