# Is there a possibility to prevent task switches in a block of code?

**URL:** <https://discourse.julialang.org/t/is-there-a-possibility-to-prevent-task-switches-in-a-block-of-code/106798>\
**Category:** Performance\
**Tags:** question, async\
**Created:** [November 27, 2023, 11:58am UTC](https://discourse.julialang.org/t/is-there-a-possibility-to-prevent-task-switches-in-a-block-of-code/106798 "2023-11-27T11:58:53Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![schlichtanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/schlichtanders/32/32145_2.png) [@schlichtanders](https://discourse.julialang.org/u/schlichtanders)\
**Post date:** [November 27, 2023, 11:58am UTC](https://discourse.julialang.org/t/is-there-a-possibility-to-prevent-task-switches-in-a-block-of-code/106798/1 "2023-11-27T11:58:53Z")

</div>

I would like to ensure that within a certain block of code no task switch can happen.

A bit like an “atomic block” for async tasks

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [November 27, 2023, 12:32pm UTC](https://discourse.julialang.org/t/is-there-a-possibility-to-prevent-task-switches-in-a-block-of-code/106798/2 "2023-11-27T12:32:08Z")

</div>

No. IIRC we don’t have a way of marking critical region

---

<div class="post-metadata">

**Author:** ![schlichtanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/schlichtanders/32/32145_2.png) [@schlichtanders](https://discourse.julialang.org/u/schlichtanders)\
**Post date:** [November 27, 2023, 1:16pm UTC](https://discourse.julialang.org/t/is-there-a-possibility-to-prevent-task-switches-in-a-block-of-code/106798/3 "2023-11-27T13:16:14Z")

</div>

is there already an issue for this feature request?

---

<div class="post-metadata">

**Author:** ![schlichtanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/schlichtanders/32/32145_2.png) [@schlichtanders](https://discourse.julialang.org/u/schlichtanders)\
**Post date:** [November 27, 2023, 1:48pm UTC](https://discourse.julialang.org/t/is-there-a-possibility-to-prevent-task-switches-in-a-block-of-code/106798/4 "2023-11-27T13:48:53Z")

</div>

I opened an issue [Feature Request: Possibility to mark certain code block as "Prevent taskswitch" · Issue #52313 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/52313)

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [November 27, 2023, 2:10pm UTC](https://discourse.julialang.org/t/is-there-a-possibility-to-prevent-task-switches-in-a-block-of-code/106798/5 "2023-11-27T14:10:55Z")

</div>

Whatever this may be, transforming code that has “yield to another task” steps at arbitrary nested calls into code that has none at all would need some internals magic for sure. The more language-level approach would be to not schedule a task to begin with or never use task-switching methods in a block. As you’re probably hinting, substituting non-switching versions so thoroughly would be a chore. For the issue’s example, there’s `Base.Libc.systemsleep`.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [November 27, 2023, 2:31pm UTC](https://discourse.julialang.org/t/is-there-a-possibility-to-prevent-task-switches-in-a-block-of-code/106798/6 "2023-11-27T14:31:48Z")

</div>

> [@schlichtanders](#):
>
> within a certain block of code no task switch can happen

Is this even possible in a non real-time OS?

---

<div class="post-metadata">

**Author:** ![caleb-allen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/caleb-allen/32/14054_2.png) [@caleb-allen](https://discourse.julialang.org/u/caleb-allen)\
**Post date:** [November 27, 2023, 2:37pm UTC](https://discourse.julialang.org/t/is-there-a-possibility-to-prevent-task-switches-in-a-block-of-code/106798/7 "2023-11-27T14:37:46Z")

</div>

Afaik task switches only occur at certain method calls, is it possible to ensure that a block of code doesn’t make any of these calls? Essentially, write a “kernel” which does the pure steps, and doesn’t make any extraneous method calls which might trigger a task switch like I/O (if that’s possible for your use case).

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [November 27, 2023, 2:38pm UTC](https://discourse.julialang.org/t/is-there-a-possibility-to-prevent-task-switches-in-a-block-of-code/106798/8 "2023-11-27T14:38:00Z")

</div>

> [@oheil](#):
>
> Is this even possible in a non real-time OS?

this is about the Julia `Task`, nothing to do with OS. But I do question the actual safety constraint this function can provide is even useful

* * *

> [@caleb-allen](#):
>
> Afaik task switches only occur at certain method calls, is it possible to ensure that a block of code doesn’t make any of these calls?

that’s not a semantic guarentee made by Julia, it can do task switch any time it wants to, in practice if you have a tight kernel task switching is unlikely to happen but that shouldn’t be the way to achive critical region. It’s not reliable anyway

---

<div class="post-metadata">

**Author:** ![schlichtanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/schlichtanders/32/32145_2.png) [@schlichtanders](https://discourse.julialang.org/u/schlichtanders)\
**Post date:** [November 27, 2023, 2:38pm UTC](https://discourse.julialang.org/t/is-there-a-possibility-to-prevent-task-switches-in-a-block-of-code/106798/9 "2023-11-27T14:38:25Z")

</div>

Intuitively whatever triggers the task-switch, maybe simply `sleep`, could have a switch to not do a taskswitch but use Base.Libc.systemsleep instead.

If it is really only about sleep, I could build a Cassette Meta transformer which replaces all invokations to sleep with invokations to Base.Libc.systemsleep. This has the disadvantage of possibly intense recompilation, but would work.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [November 27, 2023, 2:39pm UTC](https://discourse.julialang.org/t/is-there-a-possibility-to-prevent-task-switches-in-a-block-of-code/106798/10 "2023-11-27T14:39:31Z")

</div>

> [@oheil](#):
>
> Is this even possible in a non real-time OS?

Don’t think we can do anything about the OS, but this is about Julia’s task scheduler

---

<div class="post-metadata">

**Author:** ![schlichtanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/schlichtanders/32/32145_2.png) [@schlichtanders](https://discourse.julialang.org/u/schlichtanders)\
**Post date:** [November 27, 2023, 2:40pm UTC](https://discourse.julialang.org/t/is-there-a-possibility-to-prevent-task-switches-in-a-block-of-code/106798/11 "2023-11-27T14:40:01Z")

</div>

the case I have is that I run a couple of Base.run() and HTTP requests and was confused about which trigger taskswitches and which not.

A `preven_taskswitch` function would be the simple solution

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [November 27, 2023, 2:41pm UTC](https://discourse.julialang.org/t/is-there-a-possibility-to-prevent-task-switches-in-a-block-of-code/106798/12 "2023-11-27T14:41:00Z")

</div>

> [@schlichtanders](#):
>
> If it is really only about sleep

Isn’t julia’s task system _cooperative_, meaning that there are a lot of yield points all over the place? I think IO is a big one that yields

```julia
function foo()
    for i = 1:10000
        print("a")
    end
end

function bar()
    for i = 1:10000
        print("b")
    end
end

t1 = @async foo()
t2 = @async bar()

```

prints

```julia
abababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababab...

```

indicating that the `print` calls yield.

---

<div class="post-metadata">

**Author:** ![schlichtanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/schlichtanders/32/32145_2.png) [@schlichtanders](https://discourse.julialang.org/u/schlichtanders)\
**Post date:** [November 27, 2023, 2:41pm UTC](https://discourse.julialang.org/t/is-there-a-possibility-to-prevent-task-switches-in-a-block-of-code/106798/13 "2023-11-27T14:41:37Z")

</div>

> [@jling](#):
>
> that’s not a semantic guarentee made by Julia, it can do task switch any time it wants to, in practice if you have a tight kernel task switching is unlikely to happen but that shouldn’t be the way to achive critical region. It’s not reliable anyway

that sounds horrible. I always thought we only need Atomics for Threads (also the documentation suggests this).  
What you are writing would destroy plenty of async based logics

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [November 27, 2023, 2:42pm UTC](https://discourse.julialang.org/t/is-there-a-possibility-to-prevent-task-switches-in-a-block-of-code/106798/14 "2023-11-27T14:42:15Z")

</div>

> [@schlichtanders](#):
>
> What you are writing would destroy plenty of async based logics

have you not seen [PSA: Thread-local state is no longer recommended](https://julialang.org/blog/2023/07/PSA-dont-use-threadid/#quickfix_replace_threads_with_threads_static) ?

---

<div class="post-metadata">

**Author:** ![schlichtanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/schlichtanders/32/32145_2.png) [@schlichtanders](https://discourse.julialang.org/u/schlichtanders)\
**Post date:** [November 27, 2023, 2:43pm UTC](https://discourse.julialang.org/t/is-there-a-possibility-to-prevent-task-switches-in-a-block-of-code/106798/15 "2023-11-27T14:43:30Z")

</div>

the link is not about async tasks, but about Threads

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [November 27, 2023, 2:45pm UTC](https://discourse.julialang.org/t/is-there-a-possibility-to-prevent-task-switches-in-a-block-of-code/106798/16 "2023-11-27T14:45:16Z")

</div>

async has the exact same issue:

```julia
julia> f(i) = (sleep(0.01); return i)
f (generic function with 1 method)

julia> state = [0]
1-element Vector{Int64}:
 0

julia> @sync for i ∈ 1:100
               @async state[1] += f(i)
             end

julia> state
1-element Vector{Int64}:
 100

```

---

<div class="post-metadata">

**Author:** ![schlichtanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/schlichtanders/32/32145_2.png) [@schlichtanders](https://discourse.julialang.org/u/schlichtanders)\
**Post date:** [November 27, 2023, 2:48pm UTC](https://discourse.julialang.org/t/is-there-a-possibility-to-prevent-task-switches-in-a-block-of-code/106798/17 "2023-11-27T14:48:27Z")

</div>

you are calling sleep

this seems to indicate that async task can indeed not be interrupted anywhere, but just at certain points like `sleep`

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [November 27, 2023, 2:49pm UTC](https://discourse.julialang.org/t/is-there-a-possibility-to-prevent-task-switches-in-a-block-of-code/106798/18 "2023-11-27T14:49:12Z")

</div>

`sleep()` can be anything! Julia does not have a list of “possibly yileding” function and guarentee everything else is non-yielding

edit:  
oh yeah, of course anything `print()` or any logging happens you’re out

---

<div class="post-metadata">

**Author:** ![schlichtanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/schlichtanders/32/32145_2.png) [@schlichtanders](https://discourse.julialang.org/u/schlichtanders)\
**Post date:** [November 27, 2023, 2:50pm UTC](https://discourse.julialang.org/t/is-there-a-possibility-to-prevent-task-switches-in-a-block-of-code/106798/19 "2023-11-27T14:50:15Z")

</div>

impressive!!!

thank you for the example. That looks like it is almost impossible to prevent task switches.

So I am back to locking 😆

---

<div class="post-metadata">

**Author:** ![schlichtanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/schlichtanders/32/32145_2.png) [@schlichtanders](https://discourse.julialang.org/u/schlichtanders)\
**Post date:** [November 27, 2023, 2:51pm UTC](https://discourse.julialang.org/t/is-there-a-possibility-to-prevent-task-switches-in-a-block-of-code/106798/20 "2023-11-27T14:51:30Z")

</div>

> [@jling](#):
>
> `sleep()` can be anything!

please link to the documentation which says so

It is pretty unbelievable for me to take your saying literal.

[Next page](https://discourse.julialang.org/t/is-there-a-possibility-to-prevent-task-switches-in-a-block-of-code/106798.md?page=2)
