#!/usr/local/bin/perl use threads; use threads::shared; use strict; use warnings; $|=1; our $db :shared; sub func() { my $new; while (1) { print STDERR "func: new iteration started\n"; sleep(1); $new = shared_clone({}); # reference to new shared hash $$new{'key'} = shared_clone([1, 2, 3]); # fill the hash { lock($db); $db = $new; $new = undef; } print STDERR "func: iteration finished\n"; } } # parent thread $db = shared_clone({}); threads->create(\&func)->detach(); while(1) { sleep(1); }