Multithreading

according to the documentation, most important external calls( SDL
&&|| System ) must be made through the ‘main’ thread

however, in bob pendelton’s Net2 library , he got around this with
mutexes and semaphores and condition signals( or whatever they’re
called )

just wondering the precise differences between these 3 and what
function they perform.

alternatively, i could tear bits out of net2 to pieces for my own
program and allow the event library to be used in different threads(
net2 is gpled, i think ), could i use the user defined events to send
special signals to my main thread? so instead of blitting in my
seconadry thread, i would just pass the information of what to blit
and where to the primary thread. that would work, no?

but if i figure out exatly whatever net2 is doing to stop concurrent
access( which is the reason i can’t call everything whenever i want,
right? ) then i can make the external calls to sdl or whatever in any
thread i choose.

( btw, i’m not actually going to perform blitting in seconary threads,
i just really want to write a multithreaded program for the
experience )

thanks, brian

Brian Barrett <brian.ripoff at gmail.com> wrote:

could i use the user defined events to send
special signals to my main thread? so instead of blitting in my
seconadry thread, i would just pass the information of what to blit
and where to the primary thread. that would work, no?

this is a very common way to handle that problem. be aware that events
might get delayed up to 10ms depending on your platform/os. this is the
reason why bob invented this FastEvent thingy for net2. (IIRC)

clemens

according to the documentation, most important external calls( SDL
&&|| System ) must be made through the ‘main’ thread

That is not entirely true. SDL is not thread safe, so you must ensure
that each each SDL function can only be executed by one thread at a
time. AND, some SDL functions can only ever be executed in the main
thread. That is more because of platform limitations than because of SDL
limitations. Note that threaded event handling can be requested (and it
works great) on Linux, but not on Windows. Some platforms let you do
some things in threads that other platforms do not let you do in
threads. So it goes!

however, in bob pendelton’s Net2 library , he got around this with
mutexes and semaphores and condition signals( or whatever they’re
called )

Yep, that is how I did it. These are standard techniques for doing inter
thread control and communication. Note that the first thing I had to do
was write thread safe wrappers for the parts of SDL_net that I use. That
makes sure that only one thread will be in those functions at any time.

It would have been better if I had made SDL_net thread safe, but that
didn’t seem reasonable at the time. (It still doesn’t seem reasonable.)
The problem is that if you mix SDL_net calls with net2 in the same
program you can get a situation where two or more threads are trying to
run the same non-thread safe code at the same time and it may cause a
crash or data corruption.

just wondering the precise differences between these 3 and what
function they perform.

I rarely say this, but please to more than just RTing the FM. If you
have no, or very limited, knowledge of these functions either buy a book
on multithreaded programming or search for and read several on line
tutorials on the subject. Multithreaded programming is a useful skill to
have, it will soon be a vital skill. But, it isn’t something you are
going to get by asking about it on the SDL mailing list. You might go
over to my mailing list at gameprogrammer.com and ask there. It is on
topic for that list.

alternatively, i could tear bits out of net2 to pieces for my own
program and allow the event library to be used in different threads(
net2 is gpled, i think ),

Not GPLed, LGPL. Much nicer. Feel free to do exactly that. It is a good
way to go.

could i use the user defined events to send
special signals to my main thread? so instead of blitting in my
seconadry thread, i would just pass the information of what to blit
and where to the primary thread. that would work, no?

That is exactly how net2 works. The thread is isolated to a special
library that sends events to the main thread through the event queue. It
is one of the best ways there is to do multithreaded programming.

One problem that people miss when writing their first multithreaded
program is that if one thread is running, then there is a good chance
that no other thread will ever run. But, threads that are waiting on the
queue aren’t running, so other threads get a chance to run. There are a
couple of design patterns for multithreaded programs that work and about
a zillion that don’t. The ones that work will be covered in any good
book on the subject.

but if i figure out exatly whatever net2 is doing to stop concurrent
access( which is the reason i can’t call everything whenever i want,
right? ) then i can make the external calls to sdl or whatever in any
thread i choose.

Sadly, no. There are some functions that can only be called in the main
thread.

( btw, i’m not actually going to perform blitting in seconary threads,
i just really want to write a multithreaded program for the
experience )

Start out with something a lot simpler than a game. Write a simple test
program that has N threads (configurable) that implements something like
a prime number sieve. Something like that will give you experience with
threads without having to deal with lots of extraneous details.
Concentrate on what you want to learn to do and then use it in a more
complex program. Trying to learn while writing the final project is a
prescription for disaster.

When I am writing a completely new project I try very hard to identify
the parts of the project I don’t know how to implement and write test
programs that demonstrate how to do each previously unsolved problem and
then finish the design of the project. Net2 is actually one of those
test programs that grew large enough that I wrote it up.

thanks, brian

You are very welcome,
	Bob PendletonOn Wed, 2005-05-11 at 22:29 +0100, Brian Barrett wrote:

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


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