Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is pushing a variable onto an array a threadsafe operation?

I have the following Perl code:

push(@myArray, $myValue);

Is the operation atomic, or will I need to use locks, if multiple threads will be performing this same operation on many threads?

like image 745
Mike Avatar asked Jul 22 '10 02:07

Mike


1 Answers

The thread safety of most functions in perl depends on their underlying C routines, and in the case of built-ins, like push there is no mention of thread safety, so you must assume it is not.

Check out the perlthrtut man page, in particular the section titled "Basic Semaphores". Using a semaphore you can enforce mutual exclusion in arbitrary sections of code.

like image 93
maerics Avatar answered Sep 28 '22 08:09

maerics