Help with threads

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Help with threads

Ron Teitelbaum

All,

 

I have three threads, the active thread and two at systemBackgroundPriority.  It appears that the primary thread is preventing the others from running (I’m running tests in SUnit, and the method in question is setup).

 

I first thought that adding Processor yield would work but it appears from the method comments, and my tests, that this doesn’t work.

 

I added (Delay forMilliseconds: 10) wait.  and this worked.

 

What is the proper way to allow background thread to operate and get some processor time when there is an active processing running.

 

Thanks for your help,

 

Ron Teitelbaum

[hidden email]



Reply | Threaded
Open this post in threaded view
|

Re: Help with threads

johnmci
Ah, well your generic smalltalk implementation has a simple scheduler.
It does a round robin scheduling on the link list of processes ready  
to run sorted by priority.

See Interpreter>>primitiveYield & Interpreter>>wakeHighestPriority
by yielding in a high priority task which is not waiting for anything  
just restarts that process and starves the lower priority processes.

If all three threads ran at the same priority then each would get  
some run time

Being smalltalk you can of course alter things and add a revised  
scheduler that uses suspend/resume or alter priorities to do process  
escalation and other
features of more advanced schedulers.

However in your case you need to evaluate what your high priority  
task is doing? Should it be running non-stop, or waiting for the  
lower priority tasks to do something?


On 21-Jan-06, at 8:30 PM, Ron Teitelbaum wrote:

> All,
>
>
>
> I have three threads, the active thread and two at  
> systemBackgroundPriority.  It appears that the primary thread is  
> preventing the others from running (I’m running tests in SUnit, and  
> the method in question is setup).
>
>
>
> I first thought that adding Processor yield would work but it  
> appears from the method comments, and my tests, that this doesn’t  
> work.
>
>
>
> I added (Delay forMilliseconds: 10) wait.  and this worked.
>
>
>
> What is the proper way to allow background thread to operate and  
> get some processor time when there is an active processing running.
>
>
>
> Thanks for your help,
>
>
>
> Ron Teitelbaum
>
> [hidden email]
>
>

--
========================================================================
===
John M. McIntosh <[hidden email]> 1-800-477-2659
Corporate Smalltalk Consulting Ltd.  http://www.smalltalkconsulting.com
========================================================================
===


Reply | Threaded
Open this post in threaded view
|

RE: Help with threads

Ron Teitelbaum
John,

In my case I'm running setup on SUnit, the high priority thread is only
waiting for all the lower priority threads to finish.  In a production
environment, having the system wait unit user processes are finished before
running background tasks is acceptable.  But for testing I just threw
SUnit>>setUp into a loop and said wait unit you get this expected result.  I
guess I could have suspended the process and waited for a signal instead.  

The comments on Processor>>yield says use to allow processing on a process
with the same priority.  Why is it that this didn't work even though they
are not the same priority?  Since A thread is at the active priority, and B
and C are at background, wouldn't a yield on A allow processor time to B and
C? (I'm assuming that there are no other higher priority threads running,
since even debugging allows the threads to finish).  I was calling yield
once each check loop.  What process was it yielding to?

I like the idea of maybe changing the controller to check processes that
want control and then use the controller loop time to bump up their priority
to give them some processor time.  Is that a good way to manage scheduling
time?  How responsive are the threads to changes in priority?

Thanks again for your help.

Ron Teitelbaum



> -----Original Message-----
> From: [hidden email] [mailto:squeak-dev-
> [hidden email]] On Behalf Of John M McIntosh
> Sent: Sunday, January 22, 2006 4:00 AM
> To: The general-purpose Squeak developers list
> Subject: Re: Help with threads
>
> Ah, well your generic smalltalk implementation has a simple scheduler.
> It does a round robin scheduling on the link list of processes ready
> to run sorted by priority.
>
> See Interpreter>>primitiveYield & Interpreter>>wakeHighestPriority
> by yielding in a high priority task which is not waiting for anything
> just restarts that process and starves the lower priority processes.
>
> If all three threads ran at the same priority then each would get
> some run time
>
> Being smalltalk you can of course alter things and add a revised
> scheduler that uses suspend/resume or alter priorities to do process
> escalation and other
> features of more advanced schedulers.
>
> However in your case you need to evaluate what your high priority
> task is doing? Should it be running non-stop, or waiting for the
> lower priority tasks to do something?
>
>
> On 21-Jan-06, at 8:30 PM, Ron Teitelbaum wrote:
>
> > All,
> >
> >
> >
> > I have three threads, the active thread and two at
> > systemBackgroundPriority.  It appears that the primary thread is
> > preventing the others from running (I'm running tests in SUnit, and
> > the method in question is setup).
> >
> >
> >
> > I first thought that adding Processor yield would work but it
> > appears from the method comments, and my tests, that this doesn't
> > work.
> >
> >
> >
> > I added (Delay forMilliseconds: 10) wait.  and this worked.
> >
> >
> >
> > What is the proper way to allow background thread to operate and
> > get some processor time when there is an active processing running.
> >
> >
> >
> > Thanks for your help,
> >
> >
> >
> > Ron Teitelbaum
> >
> > [hidden email]
> >
> >
>
> --
> ========================================================================
> ===
> John M. McIntosh <[hidden email]> 1-800-477-2659
> Corporate Smalltalk Consulting Ltd.  http://www.smalltalkconsulting.com
> ========================================================================
> ===
>



Reply | Threaded
Open this post in threaded view
|

Re: Help with threads

johnmci

On 22-Jan-06, at 4:40 PM, Ron Teitelbaum wrote:

> The comments on Processor>>yield says use to allow processing on a  
> process
> with the same priority.  Why is it that this didn't work even  
> though they
> are not the same priority?  Since A thread is at the active  
> priority, and B
> and C are at background, wouldn't a yield on A allow processor time  
> to B and
> C?

What happens is that thread A looks for another thread to run in it's  
priority group, if any exist it
sticks itself last on the that priority list and calls  
wakeHighestPriority which does what it's  name suggest, find the  
highest priority task that can run.
If no other processes in it's priority group are available it does  
not switch.

I'll note the comment in yield says:
"Give other Processes at the current priority a chance to run."

A bit misleading since the intent is to given processes at the  
current priority a chance to run, but when it attempts to do that it  
may run a higher priority thread instead.
However it does NOT run lower priority tasks.

--
========================================================================
===
John M. McIntosh <[hidden email]> 1-800-477-2659
Corporate Smalltalk Consulting Ltd.  http://www.smalltalkconsulting.com
========================================================================
===


Reply | Threaded
Open this post in threaded view
|

Re: Help with threads

timrowledge
In reply to this post by Ron Teitelbaum

On 22-Jan-06, at 4:40 PM, Ron Teitelbaum wrote:

>
> The comments on Processor>>yield says use to allow processing on a  
> process
> with the same priority.  Why is it that this didn't work even  
> though they
> are not the same priority?  Since A thread is at the active  
> priority, and B
> and C are at background, wouldn't a yield on A allow processor time  
> to B and
> C? (I'm assuming that there are no other higher priority threads  
> running,
> since even debugging allows the threads to finish).  I was calling  
> yield
> once each check loop.  What process was it yielding to?

The yield gives other processes at the same priority a chance to run,  
nothing more. If there are not any waiting processes in the same  
priority, nothing changes.

Now, one needs to temper that statement a little with the rarely  
mentioned basic assumption built deep into the VM that a high  
priority process will pre-empt any lower priority at any message send  
or backward branch bytecode (added so that tight loops shouldn't  
screw us up) in order to get the full story. Actually these days the  
check is done every few thousand message sends or so since we enjoy  
considerably faster machines (in most cases) that used to be the  
case. In an ideal world there would be a nice way to interrupt the  
interpreter loop rather than essentially polling for something  
awaiting attention.

The only way to give a low priority process a chance to run is to put  
any and all higher priority processes to sleep for a while which  
removes them from the 'ready to run' lists which is why using a wait  
works for you. Waiting for a Delay or waiting for a semaphore signal;  
either will work.

>
> I like the idea of maybe changing the controller to check processes  
> that
> want control and then use the controller loop time to bump up their  
> priority
> to give them some processor time.  Is that a good way to manage  
> scheduling
> time?  How responsive are the threads to changes in priority?
Ah, good question. I think the only way you would get any real effect  
from changing the priority of a running or quiescent process would be  
to sleep it and reawaken it after the priority change. If you look at  
the Process>priority: method you'll see that there is nothing done to  
move the affected process around in any processor queues. Maybe that  
should be changed.

tim
--
tim Rowledge; [hidden email]; http://www.rowledge.org/tim
A)bort, R)etry or S)elf-destruct?



Reply | Threaded
Open this post in threaded view
|

RE: Help with threads

Terry Raymond-2


> -----Original Message-----
> From: [hidden email] [mailto:squeak-dev-
> [hidden email]] On Behalf Of tim Rowledge
> Sent: Monday, January 23, 2006 12:48 AM
> To: The general-purpose Squeak developers list
> Subject: Re: Help with threads

> > I like the idea of maybe changing the controller to check processes
> > that
> > want control and then use the controller loop time to bump up their
> > priority
> > to give them some processor time.  Is that a good way to manage
> > scheduling
> > time?  How responsive are the threads to changes in priority?

> Ah, good question. I think the only way you would get any real effect
> from changing the priority of a running or quiescent process would be
> to sleep it and reawaken it after the priority change. If you look at
> the Process>priority: method you'll see that there is nothing done to
> move the affected process around in any processor queues. Maybe that
> should be changed.

Definitely. It should probably be a primitive because I would also
expect if the priority were changed to one higher than the active
process that it would become the new active process immediately.

> tim
> --
> tim Rowledge; [hidden email]; http://www.rowledge.org/tim
> A)bort, R)etry or S)elf-destruct?
>