I have install julia 1.3 for the very first time and it (rc5) works
using Base.Threads
struct WorkDetail
t::Int64
data::Vector{Float64}
end
@show rawdata = rand(10)
NumOfThreads = 4
chunksize = length(rawdata) ÷ NumOfThreads
WorkDetailArr = WorkDetail[]
for t = 1:NumOfThreads
if t < NumOfThreads
push!( WorkDetailArr, WorkDetail(t, rawdata[chunksize*(t-1)+1:chunksize*(t)+1-1]) )
else
push!( WorkDetailArr, WorkDetail(t, rawdata[chunksize*(t-1)+1:length(rawdata)]) )
end
end
@show WorkDetailArr
Completed = Bool[ false for _ = 1:NumOfThreads ]
Accepted = Bool[ false for _ = 1:NumOfThreads ]
WorkTask(t) = begin
for c = 1:length(WorkDetailArr[t].data)
println("Thread $(t), Working with data = $(WorkDetailArr[t].data[c])")
sleep((NumOfThreads - t + 1) * rand())
end
"Signal that this thread has completed"
Completed[t] = true
println("Thread $(t) completion has been signaled")
end
Workers = Task[]
for t = 1:NumOfThreads
push!(Workers, Threads.@spawn WorkTask(t))
println("Thread $(t) has been spawned")
end
# while Accepted contains at least one element which is false
while ! all(Accepted)
for t = 1:length(Workers)
if xor(Completed[t],Accepted[t])
Threads.wait(Workers[t])
println("Thread $(t) completion has been accepted")
"Mark that controller has accepted the thread completion"
Accepted[t] = true
end
end
sleep(0.05) # Sleep for 50 milisecond
end
println("Done")
_
_ _ _(_)_ | Documentation: https://docs.julialang.org
(_) | (_) (_) |
_ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.
| | | | | | |/ _ | |
| | |_| | | | (_| | | Version 1.3.0-rc5.1 (2019-11-17)
_/ |\__ _|_|_|\__ _| | Official https://julialang.org/ release
|__/ |
rawdata = rand(10) = [0.0286802098414336, 0.7032722953781154, 0.77001289603403, 0.8016182959533023, 0.8425712364199844, 0.03730465628281987, 0.28679744950075947, 0.4557346403940581, 0.9531335992120678, 0.09736993766309787]
WorkDetailArr = WorkDetail[WorkDetail(1, [0.0286802098414336, 0.7032722953781154]), WorkDetail(2, [0.77001289603403, 0.8016182959533023]), WorkDetail(3, [0.8425712364199844, 0.03730465628281987]), WorkDetail(4, [0.28679744950075947, 0.4557346403940581, 0.9531335992120678, 0.09736993766309787])]
Thread 1 has been spawned
Thread 2 has been spawned
Thread 3 has been spawned
Thread 4 has been spawned
Thread 1, Working with data = 0.0286802098414336
Thread 2, Working with data = 0.77001289603403
Thread 3, Working with data = 0.8425712364199844
Thread 4, Working with data = 0.28679744950075947
Thread 4, Working with data = 0.4557346403940581
Thread 3, Working with data = 0.03730465628281987
Thread 4, Working with data = 0.9531335992120678
Thread 3 completion has been signaled
Thread 3 completion has been accepted
Thread 4, Working with data = 0.09736993766309787
Thread 2, Working with data = 0.8016182959533023
Thread 4 completion has been signaled
Thread 4 completion has been accepted
Thread 1, Working with data = 0.7032722953781154
Thread 2 completion has been signaled
Thread 2 completion has been accepted
Thread 1 completion has been signaled
Thread 1 completion has been accepted
Done