Use sleep in thread

classic Classic list List threaded Threaded
36 messages Options
12
Reply | Threaded
Open this post in threaded view
|

Use sleep in thread

Xiangrong Fang
Hi All,

Can I use Sleep() in a thread to give up cpu time to other threads running at the same time, so as to adjust the relative "niceness" of a group of workers working on the same subject (in which each thread take part of the whole task).

Thanks!

Xiangrong

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Use sleep in thread

dmitry boyarintsev
I presume most of the systems would make the sleeping thread to yield the execution time for other threads.
The questionable behavior might occur in case of  sleep(0); (should it yield the remaining time or just return immediately - up to the OS).
And multi-cpu might also do something different.

thanks,
Dmitry


On Wed, Feb 25, 2015 at 9:41 AM, Xiangrong Fang <[hidden email]> wrote:
Hi All,

Can I use Sleep() in a thread to give up cpu time to other threads running at the same time, so as to adjust the relative "niceness" of a group of workers working on the same subject (in which each thread take part of the whole task).

Thanks!

Xiangrong

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Use sleep in thread

Xiangrong Fang
2015-02-25 22:47 GMT+08:00 Dmitry Boyarintsev <[hidden email]>:
I presume most of the systems would make the sleeping thread to yield the execution time for other threads.
The questionable behavior might occur in case of  sleep(0); (should it yield the remaining time or just return immediately - up to the OS).
And multi-cpu might also do something different.

​Yes, I am particularly interested in behavior of SMP system. i.e. my purpose of using threads is to take full advantages of all CPU cores.

As far as I know, setting thread priority does not always work, it may require root privilege for example.  I would like to use Sleep() to control relative time share of all threads in the pool.  For example, I run 3 threads on a dual-core system, with thread 1 and 2 share core-1, and thread 3 taking all computing power of core-2...

Now the problem is, can I use Sleep to control thread 1 to run at 50% of the speed of thread 2 (which is not throttled), providing that all threads are doing same kind of task, so that they are comparable?

Xiangrong​
 

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Use sleep in thread

hinst
not sure if this helps, but:
for example, if you want thread T to run using approx. 70% of max. available capacity, try this:

repeat
  T.Resume;
  Sleep(70);
  T.Resume;
  Sleep(30);
until ...

so T runs for 70 ms, then sleeps for 30 ms, etc

25.02.2015, 18:06, "Xiangrong Fang" <[hidden email]>:

> 2015-02-25 22:47 GMT+08:00 Dmitry Boyarintsev <[hidden email]>:
>> I presume most of the systems would make the sleeping thread to yield the execution time for other threads.
>> The questionable behavior might occur in case of  sleep(0); (should it yield the remaining time or just return immediately - up to the OS).
>> And multi-cpu might also do something different.
> ​Yes, I am particularly interested in behavior of SMP system. i.e. my purpose of using threads is to take full advantages of all CPU cores.
>
> As far as I know, setting thread priority does not always work, it may require root privilege for example.  I would like to use Sleep() to control relative time share of all threads in the pool.  For example, I run 3 threads on a dual-core system, with thread 1 and 2 share core-1, and thread 3 taking all computing power of core-2...
>
> Now the problem is, can I use Sleep to control thread 1 to run at 50% of the speed of thread 2 (which is not throttled), providing that all threads are doing same kind of task, so that they are comparable?
>
> Xiangrong​
>
> ,
>
> _______________________________________________
> fpc-pascal maillist  -  [hidden email]
> http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Use sleep in thread

hinst
I mean T.Suspend, so u resume it then suspend it

25.02.2015, 18:16, "[hidden email]" <[hidden email]>:

> not sure if this helps, but:
> for example, if you want thread T to run using approx. 70% of max. available capacity, try this:
>
> repeat
>   T.Resume;
>   Sleep(70);
>   T.Resume;
>   Sleep(30);
> until ...
>
> so T runs for 70 ms, then sleeps for 30 ms, etc
>
> 25.02.2015, 18:06, "Xiangrong Fang" <[hidden email]>:
>>  2015-02-25 22:47 GMT+08:00 Dmitry Boyarintsev <[hidden email]>:
>>>  I presume most of the systems would make the sleeping thread to yield the execution time for other threads.
>>>  The questionable behavior might occur in case of  sleep(0); (should it yield the remaining time or just return immediately - up to the OS).
>>>  And multi-cpu might also do something different.
>>  ​Yes, I am particularly interested in behavior of SMP system. i.e. my purpose of using threads is to take full advantages of all CPU cores.
>>
>>  As far as I know, setting thread priority does not always work, it may require root privilege for example.  I would like to use Sleep() to control relative time share of all threads in the pool.  For example, I run 3 threads on a dual-core system, with thread 1 and 2 share core-1, and thread 3 taking all computing power of core-2...
>>
>>  Now the problem is, can I use Sleep to control thread 1 to run at 50% of the speed of thread 2 (which is not throttled), providing that all threads are doing same kind of task, so that they are comparable?
>>
>>  Xiangrong​
>>
>>  ,
>>
>>  _______________________________________________
>>  fpc-pascal maillist  -  [hidden email]
>>  http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Use sleep in thread

Henry Vermaak
In reply to this post by Xiangrong Fang
On Wed, Feb 25, 2015 at 10:41:58PM +0800, Xiangrong Fang wrote:
> Hi All,
>
> Can I use Sleep() in a thread to give up cpu time to other threads running
> at the same time, so as to adjust the relative "niceness" of a group of
> workers working on the same subject (in which each thread take part of the
> whole task).

Use ThreadSwitch to yield CPU.  Having said that, it may be better to
look into platform specific thread scheduling rather than trying to do
it yourself.  For instance, with pthreads, you can do this:

procedure TYourThread.SetPriority;
{$ifdef unix}
var
        sp: sched_param;
begin
        sp.sched_priority := 10;
        { SCHED_FIFO = 1 }
        pthread_setschedparam(ThreadID, 1, @sp);
end;
{$else}
begin
        // TODO
end;
{$endif}

Henry
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Use sleep in thread

Michael Schnell
In reply to this post by Xiangrong Fang
On 02/25/2015 03:41 PM, Xiangrong Fang wrote:

Can I use Sleep() in a thread to give up cpu time to other threads running at the same time
AFAIK:

Sleep(n) makes the thread give up CPU for _at_least_ n milliseconds
 
Sleep(0) makes it give up it's current time slice and is due to be re-scheduled rather soon.

sleep will not necessary give time to other threads. it also might give the time to other programs running on the system.

In an SMP OS multiple threads run at the same time, anyway. Sleep might make a thread run that before was waiting for a CPU.

-Michael

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Use sleep in thread

Henry Vermaak
In reply to this post by hinst
On Wed, Feb 25, 2015 at 06:17:17PM +0300, [hidden email] wrote:
> I mean T.Suspend, so u resume it then suspend it

TThread.Suspend and TThread.Resume are deprecated (since 2.4.4), don't
use them.

Henry
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Use sleep in thread

Xiangrong Fang
In reply to this post by hinst
2015-02-25 23:16 GMT+08:00 <[hidden email]>:
not sure if this helps, but:
for example, if you want thread T to run using approx. 70% of max. available capacity, try this:

repeat
  T.Resume;
  Sleep(70);
  T.Resume;
  Sleep(30);
until ...

​This seems not what I want. I would like to schedule it from within the thread.  My demo program is here:


TAsyncDo is the class I wrote to have a procedure run in parallel. 

I want to control priority of the thread from within the worker, not from the main thread. ​
 

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Use sleep in thread

philippe sylvain levi

synchronising tools do not do what you want?



De: [hidden email] <[hidden email]> em nome de Xiangrong Fang <[hidden email]>
Enviado: quarta-feira, 25 de fevereiro de 2015 12:33
Para: FPC-Pascal users discussions
Assunto: Re: [fpc-pascal] Use sleep in thread
 
2015-02-25 23:16 GMT+08:00 <[hidden email]>:
not sure if this helps, but:
for example, if you want thread T to run using approx. 70% of max. available capacity, try this:

repeat
  T.Resume;
  Sleep(70);
  T.Resume;
  Sleep(30);
until ...

​This seems not what I want. I would like to schedule it from within the thread.  My demo program is here:


TAsyncDo is the class I wrote to have a procedure run in parallel. 

I want to control priority of the thread from within the worker, not from the main thread. ​
 

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Use sleep in thread

fredvs
In reply to this post by Xiangrong Fang
Hello.

You may use RTLEvents to pause a thread. =>

Program Project1;

{$mode objfpc}{$H+}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  Classes;

Type

  { THelloThread }

  THelloThread = class(TThread)
    fSleepTime,fThreadNum:integer;
    fEvent:PRTLEvent;
    procedure Execute; override;
    constructor Create(sleepTime,threadNum:integer);
    destructor Destroy; override;
  end;

var

  ThreadArray:array[1..10] of THelloThread;

{ THelloThread }

procedure THelloThread.Execute;
begin
  while true do
  begin
    writeln('Thread ',fThreadNum,' sleeping for ',fsleepTime,' ms');
    RTLeventWaitFor(fEvent,fSleepTime);
    if Terminated then break;
    //don't forget to reset,
    RTLeventResetEvent(fEvent);
    writeln('Hello From Thread ',fThreadNum);
  end;
  writeln('Bye from ',fThreadNum);
end;

constructor THelloThread.Create(sleepTime,threadNum: integer);
begin
  inherited Create(true);
  FreeOnTerminate := true;
  fEvent := RTLEventCreate;
  fSleepTime := sleepTime;
  fThreadNum := threadNum;
  Start;
end;

destructor THelloThread.Destroy;
begin
  RTLeventdestroy(fEvent);
  inherited Destroy;
end;

procedure CreateThreads;
var
  l:integer;
begin
  for l := low(ThreadArray) to high(ThreadArray) do
    ThreadArray[l] := THelloThread.Create(l*1000,l);
end;

procedure StopThreads;
var
  l:integer;
begin
  for l := low(ThreadArray) to high(ThreadArray) do
  begin
    ThreadArray[l].Terminate;
    RTLeventSetEvent(ThreadArray[l].fEvent); //wake event up for early terminattion
    ThreadArray[l].WaitFor;
  end;
end;

begin
  CreateThreads;
  writeln('Press Any Key To Stop.');
  readln;
  StopThreads;
  writeln('All threads stopped');
  readln;
end.
Many thanks ;-)
Reply | Threaded
Open this post in threaded view
|

Re: Use sleep in thread

Michael Schnell
On 02/25/2015 09:17 PM, fredvs wrote:
> Hello.
>
> You may use RTLEvents to pause a thread. =>
>
Obviously CPU the overhead is a lot greater than with just calling sleep.

-Michael
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Use sleep in thread

Marco van de Voort
In our previous episode, Michael Schnell said:
> > You may use RTLEvents to pause a thread. =>
> >
> Obviously CPU the overhead is a lot greater than with just calling sleep.

??? Afaik they are the procedural counterpart of TEvents, and I think that
using them is cheaper than sleep, since you only unblock when needed.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Use sleep in thread

Michael Schnell
On 02/26/2015 10:50 AM, Marco van de Voort wrote:
> ??? Afaik they are the procedural counterpart of TEvents, and I think
> that using them is cheaper than sleep, since you only unblock when
> needed.

??? I _thought_ sleep would just call the appropriate OS function but in
fact it does

Function  FpNanoSleep  (req : ptimespec;rem : ptimespec):cint; external
name 'FPC_SYSC_NANOSLEEP';

I don't know what this does.

-Michael


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Use sleep in thread

Michael Schnell
On 02/26/2015 11:14 AM, Michael Schnell wrote:
>
>
> I don't know what this does.
With stepping in ASM I verified that (after some calculation) it does
just a single syscall (via a "sysenter" Assembler instruction).

so the overhead is minimal.

-Michael
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Use sleep in thread

Henry Vermaak
On Thu, Feb 26, 2015 at 11:20:41AM +0100, Michael Schnell wrote:
> On 02/26/2015 11:14 AM, Michael Schnell wrote:
> >I don't know what this does.
> With stepping in ASM I verified that (after some calculation) it
> does just a single syscall (via a "sysenter" Assembler instruction).
>
> so the overhead is minimal.

Blocking with an even will sleep until someone wakes you up, it's very
efficient.  Using sleep() you will have to wake up, check if someone
needs you, go to sleep again, etc.  This is obviously more inefficient.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Use sleep in thread

Michael Schnell
On 02/26/2015 11:33 AM, Henry Vermaak wrote:
> Blocking with an even will sleep until someone wakes you up, it's very
> efficient. Using sleep() you will have to wake up, check if someone
> needs you, go to sleep again, etc. This is obviously more inefficient.

We were talking about the overhead of a "sleep for n milliseconds"
functionality itself, and not about waiting for some event.

Abusing it for polling of course is not what sleep is meant for.

Sleep is for granting the CPU for other processes for (at least) a
predefined time.

See the mail of the original poster: this is what he asked for.

-Michael


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Use sleep in thread

Henry Vermaak
On Thu, Feb 26, 2015 at 11:40:44AM +0100, Michael Schnell wrote:

> On 02/26/2015 11:33 AM, Henry Vermaak wrote:
> >Blocking with an even will sleep until someone wakes you up, it's
> >very efficient. Using sleep() you will have to wake up, check if
> >someone needs you, go to sleep again, etc. This is obviously more
> >inefficient.
>
> We were talking about the overhead of a "sleep for n milliseconds"
> functionality itself, and not about waiting for some event.
>
> Abusing it for polling of course is not what sleep is meant for.
>
> Sleep is for granting the CPU for other processes for (at least) a
> predefined time.
>
> See the mail of the original poster: this is what he asked for.

The message you replied to was regarding pausing a thread, let me quote
you back to yourself:

===============================================================================

On 02/25/2015 09:17 PM, fredvs wrote:
> Hello.
>
> You may use RTLEvents to pause a thread. =>
>
Obviously CPU the overhead is a lot greater than with just calling sleep.

-Michael

===============================================================================

This is incorrect, since if you pause a thread with sleep(), you'll have
to loop to check when you're supposed to wake up.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Use sleep in thread

Michael Schnell
On 02/26/2015 11:50 AM, Henry Vermaak wrote:
>
> Obviously CPU the overhead is a lot greater than with just calling sleep.
>
..... Regarding the context ;-)

-Michael
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Use sleep in thread

Xiangrong Fang
In reply to this post by Henry Vermaak
2015-02-26 18:50 GMT+08:00 Henry Vermaak <[hidden email]>:
>
> Sleep is for granting the CPU for other processes for (at least) a
> predefined time.
>
> See the mail of the original poster: this is what he asked for.


​Thats ri​ght.


This is incorrect, since if you pause a thread with sleep(), you'll have
to loop to check when you're supposed to wake up.

​I use rtlevent, but only meant to maintain a pool of threads, while there is no task for a thread it is blocked waiting for an event.

BUT, that cannot be used to prioritize a thread. While you block a thread using rtlevent, it can only be unblocked from another thread. While you use sleep(), it still get time share of the CPU, only that it does nothing until sleep finishes, right?  That way, it only consume a tiny amount of computing power while sleeping. This, in my view, is the price I have to pay to achieve my purpose?

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
12