updates/Maple17/ThreadLocalData - Help

Online Help

All Products    Maple    MapleSim


Home : Support : Online Help : updates/Maple17/ThreadLocalData

Thread Local Data

Maple 17 has support for declaring variables that can store a different value for each thread that accesses the variable.  This allows algorithms to be implemented that need to maintain state, without needing to worrying about that state being modified by other threads.  This makes it easier to implement thread safe versions of those algorithms.  

 

A module that wants to maintain internal state generally will not work correctly when executed in parallel. Consider the following example: the Mapper module maintains an internal state using the variables func and data.  The setMapper routine is used to set these variables. When ModuleApply is called, it maps func over the given data.

 

Mapper := module ()
 local func, data;
 export setMapper, ModuleApply;
  setMapper := proc (f::procedure, d::anything)
 func := f; data := d;
 NULL
end proc;
ModuleApply := proc (d)
 map(func, d, data)
 end proc
end module:

 

adder := proc ()
 Mapper:-setMapper(proc (x, y) options operator, arrow; x+y end proc, 1);
 Mapper([seq(i, i = 1 .. 10)])
end proc:

 

adder();

2,3,4,5,6,7,8,9,10,11

(1)

 

multer := proc ()
 Mapper:-setMapper(proc (x, y) options operator, arrow; x*y end proc, 3);
 Mapper([seq(i, i = 1 .. 10)])
 end proc:

multer();

3,6,9,12,15,18,21,24,27,30

(2)

 

If you execute the adder and multer commands in parallel, you can get incorrect results. Executing the following statement multiple times will show different results, some of which will be incorrect.

   

Threads:-Task:-Start(passed, Task = [adder], Task = [multer]);

2,3,4,5,6,7,8,9,10,11,2,3,4,5,6,7,8,9,10,11

(3)

Threads:-Task:-Start(passed, Task = [adder], Task = [multer]);

2,3,4,5,6,7,8,9,10,11,3,6,9,12,15,18,21,24,27,30

(4)

Threads:-Task:-Start(passed, Task = [adder], Task = [multer]);

2,3,4,5,6,7,8,9,10,11,3,6,9,12,15,18,21,24,27,30

(5)

Threads:-Task:-Start(passed, Task = [adder], Task = [multer]);

2,3,4,5,6,7,8,9,10,11,3,6,9,12,15,18,21,24,27,30

(6)

Threads:-Task:-Start(passed, Task = [adder], Task = [multer]);

2,3,4,5,6,7,8,9,10,11,3,6,9,12,15,18,21,24,27,30

(7)

 

Incorrect results are computed because the func and data state variables are shared between threads. Thus when one thread changes func's value, the other thread is also affected. In Maple 17, this can be fixed by declaring the state variables as thread_local. This means that each thread will maintain its own value for the state variables. Thus changing the value in one thread will not effect other threads.

 

MapperTL := module ()
 local func::thread_local, data::thread_local;
 export setMapper, ModuleApply;
   setMapper := proc (f::procedure, d::anything)
 func := f; data := d;
 NULL
 end proc;
 ModuleApply := proc (d) map(func, d, data)
 end proc
 end module;

MapperTL:=modulelocalfunc::thread_local,data::thread_local;exportsetMapper,ModuleApply;end module

(8)

adderTL := proc ()
 MapperTL:-setMapper(proc (x, y) options operator, arrow; x+y end proc, 1);
 MapperTL([seq(i, i = 1 .. 10)])
 end proc;

adderTL:=procMapperTL:-setMapperx,y→x+y,1;MapperTLseqi,i=1..10end proc

(9)

adderTL();

2,3,4,5,6,7,8,9,10,11

(10)

 

multerTL := proc ()
 MapperTL:-setMapper(proc (x, y) options operator, arrow; x*y end proc, 3);
 MapperTL([seq(i, i = 1 .. 10)])
 end proc;

multerTL:=procMapperTL:-setMapperx,y→x*y,3;MapperTLseqi,i=1..10end proc

(11)

multerTL();

3,6,9,12,15,18,21,24,27,30

(12)

 

Executing this version, using the thread local variables, will return the expected result.

 

Threads:-Task:-Start(passed, Task = [adderTL], Task = [multerTL]);

2,3,4,5,6,7,8,9,10,11,3,6,9,12,15,18,21,24,27,30

(13)

Threads:-Task:-Start(passed, Task = [adderTL], Task = [multerTL]);

2,3,4,5,6,7,8,9,10,11,3,6,9,12,15,18,21,24,27,30

(14)

Threads:-Task:-Start(passed, Task = [adderTL], Task = [multerTL]);

2,3,4,5,6,7,8,9,10,11,3,6,9,12,15,18,21,24,27,30

(15)

Threads:-Task:-Start(passed, Task = [adderTL], Task = [multerTL]);

2,3,4,5,6,7,8,9,10,11,3,6,9,12,15,18,21,24,27,30

(16)

Threads:-Task:-Start(passed, Task = [adderTL], Task = [multerTL]);

2,3,4,5,6,7,8,9,10,11,3,6,9,12,15,18,21,24,27,30

(17)
 

See Also

thread_local


Download Help Document

Was this information helpful?



Please add your Comment (Optional)
E-mail Address (Optional)
What is ? This question helps us to combat spam