Skip to content

Document that BitArray isn't threadsafe #33750

@IanButterworth

Description

@IanButterworth

I'm seeing some cases (in 1.3-rc4) where Threads.@threads is missing some of the iteration assignments. With this example, 1 or a few are missed most of the times I run this.

Wrapping vals[i] = false in a spinlock fixes this, but I was under the impression that a @threads approach like this, where each element in the vals array is accessed once, shouldn't need a spinlock to avoid race conditions.

n = 10
vals = trues(n)
threadsUsed = Int[]
Threads.@threads for i = 1:n
    vals[i] = false
    !in(Threads.threadid(), threadsUsed) && push!(threadsUsed, Threads.threadid())
    sleep(0.1)
end
if any(vals) #Check that all elements have been iterated (set to false)
    @show vals
    @show threadsUsed
    @show Threads.nthreads()
end
vals = Bool[0, 0, 0, 0, 0, 0, 1, 0, 0, 0]
threadsUsed = [1, 2, 3, 5, 4]
Threads.nthreads() = 5

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsThis change adds or pertains to documentationmultithreadingBase.Threads and related functionality

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions