in reply to Re: perl threads causing high cpu usage
in thread perl threads causing high cpu usage

U mean to say that using Acme with the other modules is the cause?

  • Comment on Re^2: perl threads causing high cpu usage

Replies are listed 'Best First'.
Re^3: perl threads causing high cpu usage
by ww (Archbishop) on Jul 16, 2010 at 09:11 UTC
    I don't think that's what AM meant... but the mish-mash you cite might do most anything with the possible exception of teaching pigs to fly.

    From the Acme::Damn documentation:

    "Acme::Damn provides a single routine, damn(), which takes a blessed reference (a Perl object), and unblesses it, to return the original reference. I can't think of any reason why you might want to do this, but just because it's of no use doesn't mean that you shouldn't be able to do it."

    It's a really good practice read the docs for all your modules. Reading the rest of those is left as an exercise to tish15. And as AM mentioned, it's really hard to help solve a problem like this (to say nothing of 'hard to find any inventive to do so') without seeing your problem code.

    Before posting code though, and before jumping to more unfounded conclusions, please see On asking for help and How do I post a question effectively?.

Re^3: perl threads causing high cpu usage
by tish15 (Initiate) on Jul 16, 2010 at 09:13 UTC

    I fugured out the problem area... My parent thread, after forking child threads is running in while(1) loop... I have done this because at times I need to send signal to the parent thread and hence need the parent thread to continue to exist...

    Is there anyway to make the parent thread dormant but continue to exist

      Use a Thread::Queue to send messages between your threads, or make your parent thread ->join the children, so it waits for them to finish.

      tish15:

      When you're using a while(1) loop, be sure to have a sleep in there when your loop has nothing to do--your thread (as you noticed) can chew up quite a bit of CPU while repeatedly looking for work. You should figure out the maximum rate of checking you want, and sleep accordingly. For example, if the source of work is human input, then you might sleep a second at a time. If you're waiting on a disk drive, you might wait 100ms, etc.

      I frequently structure task loops something like this:

      while (1) { if ($task_1_ready) { do_task_1(...); } elsif ($task_2_ready) { do_task_2(...); } else { sleep($doze_time); } }

      This way, the loop does exactly one task per iteration, and sleep only when there are no tasks ready.

      ...roboticus