SDL_Delay and threads

hey everyone, got another question about threads.

If i have a program and do an SDL_Delay in the main threads will that also
delay the other threads?

This is what seems to happen in a program im making but im not sure if thats
the cause.

If so, is there a way to just make the main thread sleep without making the
child threads sleep?

Thanks!
Alan

Alan Wolfe wrote:

hey everyone, got another question about threads.

If i have a program and do an SDL_Delay in the main threads will that also
delay the other threads?

No. - unix pthread and win32 should use kernel threads which only sleep per thread.

This is what seems to happen in a program im making but im not sure if thats
the cause.
if it is not on another OS, it is probably not the cause.

If so, is there a way to just make the main thread sleep without making the
child threads sleep?
There are some cond/sem*Timeout functions to try out.

cheers, guido

hey everyone, got another question about threads.

If i have a program and do an SDL_Delay in the main threads will that also
delay the other threads?

It shouldn’t.

This is what seems to happen in a program im making but im not sure if thats
the cause.

If so, is there a way to just make the main thread sleep without making the
child threads sleep?

The only way to make sure that a thread sleeps while another thread runs
is for the first thread to wait on a semaphore or event that will be
generated by the second thread.

In general, if there are N (where N > 1) threads that can run one of
them will be run. That lucky thread will keep running until it blocks.
If it never blocks, it will never stop running. The other poor unlucky
threads will never get to run.

Using a “delay” call in your threads will block the running thread,
which will force another thread to start running. But, it will not
ensure that every thread will get to run. Using delay calls is always
the wrong solution in thread programming.

Most people expect threads to act like processes where the OS makes sure
that the processes all get to run. That isn’t the way threads work. You
have to schedule them your self using semaphores and events.

	Bob Pendleton

P.S.

I really wish that threads work the way people expect them to work. But,
they don’t they work the way they work.On Sun, 2004-03-21 at 17:27, Alan Wolfe wrote:

Thanks!
Alan


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

±--------------------------------------+

Bob Pendleton wrote:

Using a “delay” call in your threads will block the running thread,
which will force another thread to start running. But, it will not
ensure that every thread will get to run. Using delay calls is always
the wrong solution in thread programming.

Most people expect threads to act like processes where the OS makes
sure that the processes all get to run. That isn’t the way threads
work. You have to schedule them your self using semaphores and events.

though you might be right in the first place, as the definition of posix
threads does not garantie any sheduling policies, real life apps tend to
work anyway. i have tried this delay thing, and if you don’t need exact
sheduling you may get away with it. if you have every thread doing the
same work, each will run aproximatly the same amount of time.

best regards …
clemens

if i have one thread sleeping using a loop until a condition is met, would
an empty while loop still take up alot of CPU?

heres my setup if this helps things…

the main thread (you know…main()) listens for connections. when a
connection occurs, it spawns a thread for each connection.

each thread handles a request, returns data and exits.

when i ran this, it ate up a BUNCH of cpu constantly and i remembered how
your loop that checks for connections should sleep for a while between
connection checks, so i put an SDL_Delay of 500ms in.

so your saying instead of the SDL_Delay, i should have a while loop that
waits until getticks is 500 higher than when the loop started maybe?

would this still eat up CPU where SDL_Delay lets other processes work during
that time?

not sure if it’s related or if it’s just how im doing the network stuffs but
when i do as ive described, if i send a request to it, it seems to have
random delays in it, like it might send back half the request’s response and
then just pause for a second and then finish the response, as if the
SDL_Delay is affecting the other threads as well maybe.> ----- Original Message -----

From: bob@pendleton.com (Bob Pendleton)
To: “SDL Mailing List”
Sent: Tuesday, March 23, 2004 7:55 AM
Subject: Re: [goodstuff] [SDL] SDL_Delay and threads

On Sun, 2004-03-21 at 17:27, Alan Wolfe wrote:

hey everyone, got another question about threads.

If i have a program and do an SDL_Delay in the main threads will that
also

delay the other threads?

It shouldn’t.

This is what seems to happen in a program im making but im not sure if
thats

the cause.

If so, is there a way to just make the main thread sleep without making
the

child threads sleep?

The only way to make sure that a thread sleeps while another thread runs
is for the first thread to wait on a semaphore or event that will be
generated by the second thread.

In general, if there are N (where N > 1) threads that can run one of
them will be run. That lucky thread will keep running until it blocks.
If it never blocks, it will never stop running. The other poor unlucky
threads will never get to run.

Using a “delay” call in your threads will block the running thread,
which will force another thread to start running. But, it will not
ensure that every thread will get to run. Using delay calls is always
the wrong solution in thread programming.

Most people expect threads to act like processes where the OS makes sure
that the processes all get to run. That isn’t the way threads work. You
have to schedule them your self using semaphores and events.

Bob Pendleton

P.S.

I really wish that threads work the way people expect them to work. But,
they don’t they work the way they work.

Thanks!
Alan


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

±--------------------------------------+


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Bob Pendleton <@Bob_Pendleton> wrote:

Using a “delay” call in your threads will block the running thread,
which will force another thread to start running. But, it will not
ensure that every thread will get to run. Using delay calls is always
the wrong solution in thread programming.

Most people expect threads to act like processes where the OS makes
sure that the processes all get to run. That isn’t the way threads
work. You have to schedule them your self using semaphores and events.

though you might be right in the first place, as the definition of posix
threads does not garantie any sheduling policies, real life apps tend to
work anyway. i have tried this delay thing, and if you don’t need exact
sheduling you may get away with it. if you have every thread doing the
same work, each will run aproximatly the same amount of time.

Yeah, it can be made to work, but what is the point? The idea of using
threads is to let work get done when the opportunity and/or need arises.
If you are using delays to schedule the threads then you could just have
well have run them sequentially in the main loop of your program. Or,
used a cooperative thread package and avoided the overhead of using
posix threads.

Not to mention that it will break when you move it to a machine that
runs at a different speed.

		Bob PendletonOn Tue, 2004-03-23 at 12:14, Clemens Kirchgatterer wrote:

best regards …
clemens


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

±--------------------------------------+

if i have one thread sleeping using a loop until a condition is met, would
an empty while loop still take up alot of CPU?

heres my setup if this helps things…

the main thread (you know…main()) listens for connections. when a
connection occurs, it spawns a thread for each connection.

No. Do not do that. Spawn one thread the waits in a select for
connections to occur. Threads are very heavy. Creating them uses a lot
of CPU time and a lot of memory. You can handle all your network traffic
with one thread.

Better yet, read http://gameprogrammer.com/net2/net2-0.html which
describes how to do what you want. I already wrote a library to do it.

	Bob PendletonOn Tue, 2004-03-23 at 12:48, Alan Wolfe wrote:

each thread handles a request, returns data and exits.

when i ran this, it ate up a BUNCH of cpu constantly and i remembered how
your loop that checks for connections should sleep for a while between
connection checks, so i put an SDL_Delay of 500ms in.

so your saying instead of the SDL_Delay, i should have a while loop that
waits until getticks is 500 higher than when the loop started maybe?

would this still eat up CPU where SDL_Delay lets other processes work during
that time?

not sure if it’s related or if it’s just how im doing the network stuffs but
when i do as ive described, if i send a request to it, it seems to have
random delays in it, like it might send back half the request’s response and
then just pause for a second and then finish the response, as if the
SDL_Delay is affecting the other threads as well maybe.

----- Original Message -----
From: “Bob Pendleton” <@Bob_Pendleton>
To: “SDL Mailing List”
Sent: Tuesday, March 23, 2004 7:55 AM
Subject: Re: [goodstuff] [SDL] SDL_Delay and threads

On Sun, 2004-03-21 at 17:27, Alan Wolfe wrote:

hey everyone, got another question about threads.

If i have a program and do an SDL_Delay in the main threads will that
also

delay the other threads?

It shouldn’t.

This is what seems to happen in a program im making but im not sure if
thats

the cause.

If so, is there a way to just make the main thread sleep without making
the

child threads sleep?

The only way to make sure that a thread sleeps while another thread runs
is for the first thread to wait on a semaphore or event that will be
generated by the second thread.

In general, if there are N (where N > 1) threads that can run one of
them will be run. That lucky thread will keep running until it blocks.
If it never blocks, it will never stop running. The other poor unlucky
threads will never get to run.

Using a “delay” call in your threads will block the running thread,
which will force another thread to start running. But, it will not
ensure that every thread will get to run. Using delay calls is always
the wrong solution in thread programming.

Most people expect threads to act like processes where the OS makes sure
that the processes all get to run. That isn’t the way threads work. You
have to schedule them your self using semaphores and events.

Bob Pendleton

P.S.

I really wish that threads work the way people expect them to work. But,
they don’t they work the way they work.

Thanks!
Alan


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

±--------------------------------------+


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

±--------------------------------------+

Bob Pendleton wrote:

though you might be right in the first place, as the definition of
posix threads does not garantie any sheduling policies, real life
apps tend to work anyway. i have tried this delay thing, and if you
don’t need exact sheduling you may get away with it. if you have
every thread doing the same work, each will run aproximatly the same
amount of time.

Yeah, it can be made to work, but what is the point? The idea of
using threads is to let work get done when the opportunity and/or need
arises. If you are using delays to schedule the threads then you could
just have well have run them sequentially in the main loop of your
program.

because sometimes you get a first solution of your problem with less
lines of code, more readable and no additionally dependencies. and
sometimes it gives you even the best compromise between codesize and
performance.

some people claim it is impossible to write correct, bugfree non
trivial software in C. :wink:

best regards …
clemens

Bob Pendleton <@Bob_Pendleton> wrote:

though you might be right in the first place, as the definition of
posix threads does not garantie any sheduling policies, real life
apps tend to work anyway. i have tried this delay thing, and if you
don’t need exact sheduling you may get away with it. if you have
every thread doing the same work, each will run aproximatly the same
amount of time.

Yeah, it can be made to work, but what is the point? The idea of
using threads is to let work get done when the opportunity and/or need
arises. If you are using delays to schedule the threads then you could
just have well have run them sequentially in the main loop of your
program.

because sometimes you get a first solution of your problem with less
lines of code, more readable and no additionally dependencies. and
sometimes it gives you even the best compromise between codesize and
performance.

Using delays to schedule threads has none of the properties you list. If
it did I would not argue with you.

some people claim it is impossible to write correct, bugfree non
trivial software in C. :wink:

Actually, some people believe that it is not possible to write correct
bug free code in any language. Poor programmers use those statements to
justify writing code that is neither correct nor bug free.

When someone says something is possible they are probably right. When
they say something is impossible they are usually just saying that it is
impossible for them. That is, they are admitting their own ignorance and
laziness.

It takes very little knowledge to use threads correctly. But, it does
take some knowledge. People who refuse knowledge when it is offered to
them find that most things are impossible.

	Bob PendletonOn Wed, 2004-03-24 at 10:58, Clemens Kirchgatterer wrote:

best regards …
clemens


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

±--------------------------------------+

Bob Pendleton escribi?:> On Tue, 2004-03-23 at 12:48, Alan Wolfe wrote:

if i have one thread sleeping using a loop until a condition is met, would
an empty while loop still take up alot of CPU?

heres my setup if this helps things…

the main thread (you know…main()) listens for connections. when a
connection occurs, it spawns a thread for each connection.

No. Do not do that. Spawn one thread the waits in a select for
connections to occur. Threads are very heavy. Creating them uses a lot
of CPU time and a lot of memory. You can handle all your network traffic
with one thread.

Strange advice, I would say. Spawning a thread for treating an incoming connection is one
of the safest and elegant ways to do it.

Creating a thread just means to allocate one page for the new thread’s stack, and one
entry on the kernel process table. As you probably know, all threads of a process share
the code, static data and heap pages.

Indeed, it’s a better (performance-wise) way to do it than the common Unix practice of
fork’ing the whole process.

Regards,
Wizord.