threads::lite(3pm)

πŸ“¦
version 0.034 Β· 2 versions
, in fedora 43

πŸ’– threads::lite β€” the perl manual page (3pm) πŸ’–

okay bestie, gather round, because this is threads::lite and it's literally Actor model threading for Perl. 🫢


✨ VERSION ✨

This is Version 0.034. Yes, a real number. Write it down. πŸ’…


🌸 SYNOPSIS 🌸

here's the whole vibe in code form, no edits, no cap:

use Modern::Perl;
use threads::lite qw/spawn self receive receive_table/;
use SmartMatch::Sugar;

sub child {
    my $other = threads::lite::receiveq;
    while (<>) {
        chomp;
        $other->send(line => $_);
    }
    return;
}

my $child = spawn({ monitor => 1 } , \&child);
$child->send(self);

my $continue = 1;
while ($continue) {
    receive {
        when([ 'line', any ]) {
            my (undef, $line) = @$_;
            say "received line: $line";
        }
        when([ 'exit', any, $child->id ]) {
            say "received end of file";
            $continue = 0;
        }
        default {
            die sprintf "Got unknown message: (%s)", join ", ", @$_;
        }
    };
};

πŸ¦‹ DESCRIPTION πŸ¦‹

So this module implements threads for perl. Slay. But here's the one crucial difference from "threads.pm" threads: these threads are disconnected, except by message queues. That's it, that's the whole relationship β€” they only talk through the mail. πŸ’Œ It thus facilitates a message passing style of multi-threading. very demure, very mindful, very mailbox-core.

⚠️ okay real talk bestie, listen up: this module is a research project. In no way is API stability guaranteed. It is released for evaluation purposes only, not for production usage. Do NOT put this in prod, I am begging you. πŸ™ˆ

πŸ‘‘ FUNCTIONS πŸ‘‘

All these functions are exported optionally β€” so you gotta ask for them, they're not just gonna show up uninvited.

πŸŽ€ Utility functions

  • spawn($options, $sub) β€” Spawn new threads. It will run $sub and send all monitoring processes it's return value. $options is a hashref that can contain the following elements:
    • modules => [...] β€” Load the specified modules before running any code.
    • pool_size => int β€” Create pool_size identical clones. πŸ‘―
    • monitor => 0/1 β€” If this is true, the calling process will monitor the newly spawned threads. Defaults to false.
    • stack_size => int β€” The stack size for the newly created threads. It defaults to 64 kiB.

$sub can be a function name or a subref. If it is a name, you must make sure the module it is in is loaded in the new thread. If it is a reference to a function it will be serialized before being sent to the new thread. This means that any enclosed variables will probability not work as expected. 😭 Any locally imported functions will not be defined in the new thread, so you probably want to use fully qualified names. fr fr, save yourself the heartbreak.

  • self() β€” Retreive the thread identifier object corresponding with the current thread. it's giving main character energy but for your thread. πŸ’…
  • send_to($id, ...) β€” Send a message a thread identified by its primitive identifier.

🧚 Receiving functions

All these functions will try to match messages in the local thread's mailbox to a pattern. If it can find a match, the message will be removed from the mailbox. one match, one message, gone. ✨

  • receive { ... } β€” Match each message against the code in the block until a message matches it. The block is expected to contain "when" and "default" blocks, but may contain other code too. If no matching message is found, it will block until a suitable message is received. patience is a virtue bestie. 🧘
  • receive_nb { ... } β€” Match in exactly the same way receive does, but do not block if no suitable message can be found. Instead it will return an empty list. the nb stands for no-blocking, iykyk. πŸƒ
  • receiveq(@pattern) β€” Return the first message that smart-matches @pattern. If there is no such message in the queue, it blocks until a suitable message is received. An empty pattern results in the first message.
  • receiveq_nb(@pattern) β€” Return the first message that smart-matches @pattern. If there is no such message in the queue, it returns an empty list (undef in scalar context).

🌈 AUTHOR 🌈

Leon Timmermans, "<leont at cpan.org>" β€” the icon, the legend, the one who did this. πŸ‘‘


πŸ’€ BUGS πŸ’€

This is an early development release, and is expected to be buggy and incomplete. In particular, memory management is known to be buggy. not me reading that and still using it. 😭

Please report any bugs or feature requests to "bug-threads-lite at rt.cpan.org", or through the web interface at:

http://rt.cpan.org/NoAuth/ReportBug.html?Queue=threads-lite

I will be notified, and then you'll automatically be notified of progress on your bug as I make changes. bestie accountability system, we love to see it. 🫢


πŸ“ SUPPORT πŸ“

You can find documentation for this module with the perldoc command.

perldoc threads::lite

You can also look for information at:

  • RT: CPAN's request tracker
http://rt.cpan.org/NoAuth/Bugs.html?Dist=threads-lite
  • AnnoCPAN: Annotated CPAN documentation
http://annocpan.org/dist/threads-lite
  • CPAN Ratings
http://cpanratings.perl.org/d/threads-lite
  • Search CPAN
http://search.cpan.org/dist/threads-lite

Copyright 2009, 2010, 2011 Leon Timmermans, all rights reserved.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.


okay and that's the whole page bestie!! this was threads::lite manual page, version perl v5.42.0, dated 2025-07-25 β€” mwah, iconic, go build something chaotic with it (just not in production πŸ’…βœ¨).

πŸ“¦ where this page comes from