Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: And now something completely different: Multithreading

Author: Robert Hyatt

Date: 07:51:40 12/01/03

Go up one level in this thread


On December 01, 2003 at 04:23:56, Tony Werten wrote:

>On December 01, 2003 at 03:39:27, Russell Reagan wrote:
>
>>On December 01, 2003 at 02:41:28, Tony Werten wrote:
>>
>>>When my masterthread is spinning, waiting for results from it's workers threads,
>>>how do I keep it from burning CPU time ?
>>>
>>>The apifunction sleep() doesn't some threadsafe, suspending the thread and
>>>having it resumed by the worker seems overly complicated. ( Accept maybe if I
>>>can use a callback function )
>>>
>>>Any thoughts ?
>>
>>What language are you using?
>>
>>The Sleep() Win32 API call might be a dirty solution. IIRC, the Sleep() function
>>puts the calling thread to sleep for a specified number of milliseconds, so that
>>thread shouldn't run during that time. I think Sleep() is different from
>>sleep(). Also check out SleepEx(). It can do some extra things that might help
>>you out.
>
>I think that'll be my first try. Something like:
>
>no_results:=true;
>
>while no_results do sleep(100,true)

I hate to sound like this, but that is so incredibly ugly that it doesn't
pass muster for a solution in an entry-level threading course.  You _never_
wait for some fixed period of time.  Why have that processor idle when there
is something useful to be done?  Suppose work arrives _immediately_ after
you start the sleep.  You sit.

In threads, you _never_ use "time" in this way.  If you need to wait for
N seconds, then wait for N seconds.  But don't wait for a period of time
when you really want to wait for a specific event to be posted.


>
>
>procedure some_results; // callback function
>begin
>   no_results:=false;
>end;
>
>Let's see if this remains hidden in the profiler list. I'll probably need to add
>some criticalsections to.
>
>Thanks,
>

It'll remain hidden, but it will waste time every time you get there.  You
will just be sleeping a lot rather than executing.

>Tony
>
>>
>>If you want to do it the "right" way, then you should probably use
>>WaitForSingleObject() or WaitForMultipleObjects(), both Win32 API functions. I
>>think these will only work if you use the Win32 threading stuff though. It's
>>been a while since I've used them, but I think those functions will wait on any
>>Windows handle (events, threads, files, etc.).
>>
>>If you use a different third party threading library and it doesn't provide this
>>functionality, Sleep() might be a good solution.
>>
>>You might have a look at these:
>>
>>Process and Thread Functions (Sleep/SleepEx, among other interesting functions)
>>http://msdn.microsoft.com/library/en-us/dllproc/base/process_and_thread_functions.asp
>>
>>Synchronization Functions (WaitFor*, among other interesting functions)
>>http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/waitforsingleobject.asp



This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.