Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

avoiding thread-unsafe code with BitArray #40096

Open
goretkin opened this issue Mar 18, 2021 · 0 comments
Open

avoiding thread-unsafe code with BitArray #40096

goretkin opened this issue Mar 18, 2021 · 0 comments
Labels
domain:arrays [a, r, r, a, y, s] domain:multithreading Base.Threads and related functionality

Comments

@goretkin
Copy link
Contributor

goretkin commented Mar 18, 2021

I had an unpleasant experience using some multithreaded code that was writing into a BitArray that had been constructed using similar on another array that was the result of broadcasting:

julia> map(iseven, 1:4)
4-element Array{Bool,1}:
 0
 1
 0
 1

julia> iseven.(1:4)
4-element BitArray{1}:
 0
 1
 0
 1

It has been documented that setindex!(::BitArray, ...) is not thread-safe. However, BitArray arises whenever broadcasting produces an array with eltype(array) == Bool, and so I think it is easy to not be aware of this issue. Personally, I have an easy time being suspicious of BitArray in threaded code, even without the explicit warning. Yet it still did not initially occur to me that I should look for a BitArray when I was experiencing a race condition. I did not even remember that there were BitArray values in my code.

Some ideas that may have helped me in this case:

  1. Broadcasting never produces BitArrays (I was originally going to title this issue "consider Array{Bool} in place of BitArray for broadcasting result")
    Or, it produces some read-only version of BitArray.
  2. similar returns Array{Bool} even for BitArray input
    Or it has an extra argument to return an array of a type that is "more" thread safe.
  3. there is some trait defined like
isthreadsafe(typeof(setindex!), ::BitArray, args...) = NoItIsNot()
isthreadsafe(typeof(setindex!), ::Array, args...) = YesItIsIndeed()
# ...

And users can use it to define generic code, or it could be checked automatically by a macro that does threading.
(related https://github.com/JuliaArrays/ArrayInterface.jl)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain:arrays [a, r, r, a, y, s] domain:multithreading Base.Threads and related functionality
Projects
None yet
Development

No branches or pull requests

2 participants