From 672f609f97c8fb51ad7e8c7178b57c23972e2374 Mon Sep 17 00:00:00 2001 From: Katharine Hyatt Date: Mon, 22 May 2023 07:06:39 -0400 Subject: [PATCH] Two simple examples for Threads (#49704) --- base/threadingconstructs.jl | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/base/threadingconstructs.jl b/base/threadingconstructs.jl index d150fd3ea1af4..714a7054313d0 100644 --- a/base/threadingconstructs.jl +++ b/base/threadingconstructs.jl @@ -8,6 +8,20 @@ export threadid, nthreads, @threads, @spawn, Get the ID number of the current thread of execution. The master thread has ID `1`. + +# Examples +```julia-repl +julia> Threads.threadid() +1 + +julia> Threads.@threads for i in 1:4 + println(Threads.threadid()) + end +4 +2 +5 +4 +``` """ threadid() = Int(ccall(:jl_threadid, Int16, ())+1) @@ -352,6 +366,17 @@ the variable's value in the current task. !!! compat "Julia 1.9" A threadpool may be specified as of Julia 1.9. + +# Examples +```julia-repl +julia> t() = println("Hello from ", Threads.threadid()); + +julia> tasks = fetch.([Threads.@spawn t() for i in 1:4]); +Hello from 1 +Hello from 1 +Hello from 3 +Hello from 4 +``` """ macro spawn(args...) tp = :default