# Making a task.sticky = false run in any thread

**URL:** https://discourse.julialang.org/t/making-a-task-sticky-false-run-in-any-thread/41396
**Category:** New to Julia
**Tags:** multithreading
**Created:** [June 14, 2020, 6:30pm UTC](https://discourse.julialang.org/t/making-a-task-sticky-false-run-in-any-thread/41396 "2020-06-14T18:30:10Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Leandro\_Bataglia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leandro_bataglia/32/15674_2.png) [@Leandro\_Bataglia](https://discourse.julialang.org/u/Leandro_Bataglia)
#### Post date: [June 14, 2020, 6:30pm UTC](https://discourse.julialang.org/t/making-a-task-sticky-false-run-in-any-thread/41396/1 "2020-06-14T18:30:10Z")

</div>

I was experimenting with multithreading to learn more about Julia’s multithreading capabilities. I tried to replicate the @spawn steps with ordinary Task creation:

```julia
function prime_check(n)
    for i = 2:n/2
        if n%i == 0
            return nothing
        end
    end
    return n
end

function primes_spawn!(A , value)
    cond = ReentrantLock()
    @sync for i = 2:value
        Threads.@spawn begin
            a = prime_check(i)
            if a != nothing
                lock(cond)
                push!(A, a)
                unlock(cond)
            end
        end
    end
end

function primes_task!(A, value::Int)
    cond = ReentrantLock()
    for i = 2:value
        let 
            local task = Task(()->prime_check(i))
            task.sticky = false
            schedule(task)
            if fetch(task) != nothing
                lock(cond)
                println(fetch(task), " running on thread ", threadid())
                #push!(A, fetch(task))
                unlock(cond)
                wait(task)
            end
        end
    end
end

```

When I run it I get the following result:

```julia
A = Int64[]
primes_task!(A, 20)

```

```julia
2 running on thread 1
3 running on thread 1
5 running on thread 1
7 running on thread 1
11 running on thread 1
13 running on thread 1
17 running on thread 1
19 running on thread 1

```

Julia code warns the following:

#Note there are three reasons a Task might be put into a sticky queue  
# even if t.sticky == false:  
# 1. The Task’s stack is currently being used by the scheduler for a certain thread.  
# 2. There is only 1 thread.  
# 3. The multiq is full (can be fixed by making it growable).

But I don’t know where to fix it to make it work correctly.

---

<div class="post-metadata">

### Author: ![Luapulu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luapulu/32/17669_2.png) [@Luapulu](https://discourse.julialang.org/u/Luapulu)
#### Post date: [September 5, 2022, 7:20am UTC](https://discourse.julialang.org/t/making-a-task-sticky-false-run-in-any-thread/41396/2 "2022-09-05T07:20:52Z")

</div>

Did you end up getting an answer?

Edit: if anybody stumbles across this, I found [this paper](https://proceedings.juliacon.org/papers/10.21105/jcon.00054.pdf) useful for understanding Julias Task system.

---

<div class="post-metadata">

### Author: ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)
#### Post date: [September 7, 2022, 8:48am UTC](https://discourse.julialang.org/t/making-a-task-sticky-false-run-in-any-thread/41396/3 "2022-09-07T08:48:33Z")

</div>

Make sure to pass `-t auto` as a command line option for Julia to launch multiple threads.
