1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use threads;
7use Thread::Semaphore;
8
9MAIN:
10{
11    # Create semaphore with count of 0
12    my $s = Thread::Semaphore->new(0);
13
14    # Create detached thread
15    threads->create(sub {
16            # Thread is blocked until released by main
17            $s->down();
18
19            # Thread does work
20            # ...
21
22            # Tell main that thread is finished
23            $s->up();
24    })->detach();
25
26    # Release thread to do work
27    $s->up();
28
29    # Wait for thread to finish
30    $s->down();
31}
32
33exit(0);
34
35# EOF
36