Re: How to make threads share CPU (almost) equally?

From: Frank (abc_at_def.ghi)
Date: 03/10/05


Date: Thu, 10 Mar 2005 11:33:46 +0100

TheOne wrote:
> I am developing a multithreaded application. I have a main thread
> (default java main thread)
> which creates and starts n number of new "worker threads" whose
> priority is set to MAX_PRIORITY at the
> time of creation.

Above normal priority might be appropriate for IO or very critical and
extremely short-lived tasks.
It doesn't make your computer any faster, you'll just be stealing cpu
time from other, perhaps more important, tasks.

> It then calls join() on each of these threads.

Hmm.. Probably ok if all the threads really are already started and you
don't need to do anything else from your main thread till all the
workers are finished. I assume joining on a finished thread will just
return immidiately.

> The run method of these threads is something like this:
> (I am giving steps here instead of actual code for space reasons)
>
> 1. //get some work to do
>
> 2. //request data over the network(IO bound operation)
>
> 3. //Do some calculations on the data (CPU bound operation)
>
> 4. //update some shared data structures
>
> Now I want the IO operations(2) of one thread going simultaneouly
> with calculations(3) of another thread (thats the whole purpose of
> multithreading !!!)
> But it does not happen. The first thread created (thread-0) does not
> give up
> CPU.

Hmm..

> I tried lowering current thread's priority to MIN_PRIORITY before
> it enters step(2) and reverting it back to MAX_PRIORITY before step(3)
> but it was of no use too.

Really? Try it the other way around...

> One thing I did not try (and would not like to write code for) is to
> make
> the currently running thread sleep for a brief amount of time between
> each IO request in
> step(2). I feel that it is not a proper way of making threads share CPU
> equally.
> Am I right?
>
> Do you have some good suggestions on how to make ensure that one thread
> does not
> monopolize CPU time even when it is waiting for IO ?

No reason to call sleep for a thread doing blocking IO.

If it's non-blocking OTOH, you want to suspend it for a while when
there's no input ready, or it will just burn cycles checking for input
over and over.

And do go over your synchronization approach, checking for deadlocks.



Relevant Pages

  • How to make threads share CPU (almost) equally?
    ... multithreading!!!) ... I tried lowering current thread's priority to MIN_PRIORITY before ... I feel that it is not a proper way of making threads share CPU ... I am using Java 1.5 on Windows XP platform. ...
    (comp.lang.java.programmer)
  • [ckpatch][3/29] 2.6.17-smpnice-staircase-16
    ... Implement the "staircase" hybrid foreground-background single priority ... array cpu scheduler policy. ... extern struct user_struct root_user; ... goto out_running; ...
    (Linux-Kernel)
  • [RFC][PATCH] O(1) Entitlement Based Scheduler
    ... This patch is a modification of the Oscheduler that introduces ... _entitlement_ to CPU resources that is determined by the number of _shares_ ... This patch provides both soft and hard CPU usage rate caps per ... one getting the most can be given a better priority, ...
    (Linux-Kernel)
  • [ANNOUNCE] RSDL completely fair starvation free interactive cpu scheduler
    ... Staircase DeadLine" cpu scheduler. ... A novel design which incorporates a foreground-background descending priority ... the cpu that it is queued onto also keeps a record of that quota. ... run at this runqueue's current priority rotation. ...
    (Linux-Kernel)
  • Re: [ANNOUNCE] RSDL completely fair starvation free interactive cpu scheduler
    ... Staircase DeadLine" cpu scheduler. ... A novel design which incorporates a foreground-background descending priority ... the cpu that it is queued onto also keeps a record of that quota. ... task uses up its quota it is decremented one priority level. ...
    (Linux-Kernel)