I am completely new to Perl, like absolute newbie. I am trying to develop a system which reads a database and, according to the results, generates a queue which launches another script.
HERE is the source code.
Now the script works as expected, except I have noticed that it doesn’t really do the threads parallel. Whether I use 1 thread or 50 threads, the execution time is the same; 1 thread is even faster.
When I have the script display which thread did what, I see the threads don’t run at the same time, because it will do thread 1, then 2, then 3 etc.
Does anyone know what I did wrong here? Again the script itself works, just not in parallel threads.
You need to learn what semaphores actually are before you start using them. You’ve explicitly told the threads not to run in parallel:
You’ve created a semaphore
$s, which by default has a count of 1. Then in the function you’re trying to run, you call$s->downat the start — which decreases the count by 1, or blocks if the count is already <1, and$s->upat the end, which increases the count by 1.Once a thread calls
down, no other threads will run until it callsupagain.You should carefully read the Thread::Semaphore docs, and probably this wikipedia article on semaphores, too.