c# - Threading and sleep() -
for game i'm working on, wanted throw audio on thread queue sounds , have them play. code i'm using in thread looks this
private void _checkforthingstoplay() { while (true) { if (_song != null) { _playsong(_song); _song = null; } while (_effects.count > 0) _playsfx(_effects.dequeue()); thread.sleep(100); } }
this runs asynchronously , works beautifully. however, without call sleep eats entire core on cpu. questiom is, sleep() efficient way lower cpu usage or there better ways this? friend , feel quick hack.
that called producer-consumer problem , can solved. if using .net 4, try blockingcollection
. there's sample code in msdn page.
note line in sample code:
while (true) console.writeline(bc.take());
that block (no cpu cycles wasted) until there consume.
Comments
Post a Comment