Skip to content

vchuravy/TaskLocalValues.jl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TaskLocalValues

Task local storage in Julia allows developers to store data within a task. TaskLocalValues.jl solves two problems of task local storage:

  1. Accesses to task local storage is not type-stable.
  2. Key's can conflict between users.

A common pattern in Julia currently is to use symbols as keys to task local storage. If two packages use the same symbol they would silently conflict. TaskLocalValues.jl does not use symbols, but rather uses the object identity of the task local value itself, gurantueeing that no silent conflicts can occur.

The TaskLocalValue struct also carries an initializer and type-information.

Usage

import Base.Threads: @spawn
using TaskLocalValues

const counter = TaskLocalValue{Int}(()->0)

@show counter[] # 0 -- initialized on first use
counter[] += 1
@show counter[] # 1
@sync begin
    @spawn begin
        @show counter[] # 0 -- each tasks gets their own copy
        counter[] += 1
    end
end

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages