Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Vexing Race Condition

by tybalt89 (Monsignor)
on May 01, 2025 at 15:44 UTC ( [id://11164891]=note: print w/replies, xml ) Need Help??


in reply to Vexing Race Condition

Try this. Path::Tiny::spew writes to a temporary file and then atomic renames to the final name. This should never allow an empty file.

#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11164885 use warnings; use Path::Tiny; #one.pl my $file = '/dev/shm/tmpcount.txt'; for (1 .. 180) { print "$_\n"; # &filewrite($file,$_); path($file)->spew($_); # NOTE writes to temp file, then atomic renam +es over sleep 1; } #two.pl my $file = '/dev/shm/tmpcount.txt'; my $c; do { if (-e $file) { # $c = &fileread($file); $c = path($file)->slurp; die "C is empty" unless ($c); print "count is $c \n"; } else { print "File not found\n"; } } until ($c == 180);

Replies are listed 'Best First'.
Re^2: Vexing Race Condition
by Maelstrom (Beadle) on May 02, 2025 at 02:17 UTC
    Of all the wheels I regret re-inventing Path::Tiny may be the most regrettable. The atomic renaming seems to have eradicated this race condition entirely. Cheers.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11164891]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2025-12-16 02:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your view on AI coding assistants?





    Results (95 votes). Check out past polls.

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.