c++ - Does a worker thread need to sleep? -



c++ - Does a worker thread need to sleep? -

a normal worker thread runs in loop in checks available tasks in queue , consumes them. i've seem examples thread waits couple of milliseconds when there no available task, using std::this_thread::sleep_for() example. however, want thread consume tasks minimal latency, don't utilize sleep. bad? should worker thread sleep milliseconds when checking tasks?

if "roll own" queue or message system, 1 way avoid consuming 100% cpu "sleep" bit when there's no work available. however, having other mechanism lets os take care of (semaphore(s), os-based queue such pipe or mailbox system, or using network message passing mechanism, etc) much better, since simplifies application, , allows os take care of sleeping/waking processing.

of course, depends on trying accomplish , expectation - if code intended run on, say, mobile phone, not wasting cpu-cycles critical, because kills battery life wasting cpu cycles used on other task. on other hand, if messaging scheme in abs brakes of car, , require measure time of pulses sensor sub-microsecond accuracy, sleeping bad idea, since code sleep lot longer 1 time pulse abs sensor.

if it's in desktop, nobody care if application doesn't sleep if runs few seconds or min or more. if keeps using 1 core @ 100% hours (and app isn't producing @ to the lowest degree valuable cycles wasted - calculating next largest prime number in world or folding proteins worth wasting cycles on. saving 0.5 milliseconds on receiving packet net isn't).

only (and other people involved in project of course) know design criteria project. not can "ask internet" about. above should give guidance 1 time have figured out design criteria is, however.

c++ multithreading sleep thread-sleep worker-thread

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -