vibe.d beta banner
get vibe.d
0.10.1

Asynchronous I/O that doesn’t get in your way, written in D

Struct TaskLocal

Implements a task local storage variable.

struct TaskLocal(T) ;

Task local variables, similar to thread local variables, exist separately in each task. Consequently, they do not need any form of synchronization when accessing them.

Note, however, that each TaskLocal variable will increase the memory footprint of any task that uses task local storage. There is also an overhead to access TaskLocal variables, higher than for thread local variables, but generelly still O(1) (since actual storage acquisition is done lazily the first access can require a memory allocation with unknown computational costs).

Constructors

NameDescription
this (init_val)

Fields

NameTypeDescription
m_hasInitValue bool
m_id size_t
m_initValue T
m_offset size_t

Properties

NameTypeDescription
storage[get] T

Methods

NameDescription
opAssign (value)

Notice

FiberLocal instances MUST be declared as static/global thread-local variables. Defining them as a temporary/stack variable will cause crashes or data corruption!

Examples

TaskLocal!string s_myString = "world";

void taskFunc()
{
	assert(s_myString == "world");
	s_myString = "hello";
	assert(s_myString == "hello");
}

shared static this()
{
	// both tasks will get independent storage for s_myString
	runTask(&taskFunc);
	runTask(&taskFunc);
}
Authors

Sönke Ludwig

Copyright

© 2012-2016 RejectedSoftware e.K.

License

Subject to the terms of the MIT license, as written in the included LICENSE.txt file.