[haiku-3rdparty-dev] Re: Multi-Threaded Code

  • From: Ingo Weinhold <ingo_weinhold@xxxxxx>
  • To: haiku-3rdparty-dev@xxxxxxxxxxxxx
  • Date: Thu, 21 Apr 2016 13:23:31 +0200

On 04/19/2016 11:11 PM, Earl Pottinger (Redacted sender earl_colby_pottinger for DMARC) wrote:

I solved my problem, but by doing it a different way than you suggested.

The original code when at rest showed that it was still using 70% CPU
time if there was no other program running.

All threads were running in a 'while' loop with a snooze(1LL) at the end
of the loop, increasing the snooze time lowed the CPU time used during
rest but make the compression slower.

Depending on the architecture the code is running on, waiting for a single microsecond might not even be possible. Polling is really not a particularly good strategy, since -- as you have noticed yourself with larger snooze times -- it introduces unnecessary latencies.

I added two additional flags THREAD_SUSPEND and THREAD_RUNNING.

When a thread received the THREAD_SUSPEND I increased the snooze time to
99LL, and when a thread receives THREAD_RUNNING the snooze time is
decreased to 0LL.  So just before compression I send a THREAD_RUNNING
flag and after I finished saving the data I send a THREAD_SUSPEND flag.

I assume by sending/receiving you mean you're using the send_data() and receive_data() API. If it is possible at all to orchestrate your threads this way, I don't understand why you need the snooze() at all. receive_data() is blocking, so you could just as well let a worker thread block in receive_data() until it is supposed to process data.

CU, Ingo


Other related posts: