Skip to content

maleadt/pthreads.jl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pthreads.jl

POSIX Threads support in Julia

This package is a proof-of-concept showing how to create and manage POSIX Threads from Julia. It is only supported on Julia 1.9 and above.

Quick start

julia> using pthreads

julia> thread = pthread() do
           println("Hello, world!")
           return 42
       end;
Hello, world!

julia> # wait for the thread in order to access its return value
       wait(thread)
42

If you don't care about the results, you can detach the thread:

julia> using pthreads

julia> thread = pthread() do
           println("Off we go!")
           return
       end;
Off we go!

julia> detach(thread)

It's also possible to cancel running threads:

julia> thread = pthread() do
           println("This will take a while...")
           sleep(999)
       end;
This will take a while...

julia> cancel(thread)

julia> # wait for the thread in order to detect any exception
       wait(thread)
ERROR: InterruptException

Note that the ability to run foreign threads is a relatively recent addition to Julia, and there may be bugs. Specifically, it is very likely that thread removal (either by cancelling threads, or by threads just finishing) can lead to corrupt state in the Julia scheduler, so it's recommended to be conservative with such operations.

About

POSIX Threads support in Julia.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages