SDL and threads, advice please

On a Linux box, I’d like to use SDL (to host OpenGL) in a program I’m
developing, preferably with threads, but some snippets I’ve seen
indicate that this might create some limitations. I’d like to have 3
threads, the master handling TCP/IP communications and decoding
instructions, one doing the graphics (SDL/OpenGL), and the third doing
some other serial communications stuff. There will be no mouse or
keyboard attached to this machine. I’d be grateful for advice about the
feasibility or otherwise of this plan. If the threads would create a
problem with SDL, would I be better off with GLUT?

thanks
Gib

On a Linux box, I’d like to use SDL (to host OpenGL) in a program I’m
developing, preferably with threads, but some snippets I’ve seen
indicate that this might create some limitations. I’d like to have 3
threads, the master handling TCP/IP communications and decoding
instructions, one doing the graphics (SDL/OpenGL), and the third doing
some other serial communications stuff. There will be no mouse or
keyboard attached to this machine. I’d be grateful for advice about the
feasibility or otherwise of this plan. If the threads would create a
problem with SDL, would I be better off with GLUT?

As long as you use the main execution thread to do all graphics, you’ll
be fine.

See ya!
-Sam Lantinga, Software Engineer, Blizzard Entertainment

?On a Linux box, I’d like to use SDL (to host OpenGL) in a program I’m
?developing, preferably with threads, but some snippets I’ve seen
?indicate that this might create some limitations. ?I’d like to have 3
?threads, the master handling TCP/IP communications and decoding
?instructions, one doing the graphics (SDL/OpenGL), and the third doing
?some other serial communications stuff. ?There will be no mouse or
?keyboard attached to this machine. ?I’d be grateful for advice about the
?feasibility or otherwise of this plan. ?If the threads would create a
?problem with SDL, would I be better off with GLUT?

As long as you use the main execution thread to do all graphics, you’ll
be fine.

where exactly is the problem with using graphics and threads? several times i heared
SDL wouldn’t be thread-safe, but i don’t know what exactly this does mean.
probably, the worst thing is i haven’t encountered any problem with that…
are there nasty race-conditions still waiting for me?

See ya!
???-Sam Lantinga, Software Engineer, Blizzard Entertainment

regards,
Tolga.

ps: oh, i almost forgot: using threads with sdl is great because of it’s nice and
simple design (in comparison to standard posix threads) …______________________________________________________________________________
All inclusive! 100 MB Speicher, werbefrei, SMS g?nstiger, Wunschrufnummer, Events,
Preisvorteile und mehr unter http://club.web.de/?mc=021104

Personally I feel using threads is a cop out :slight_smile: In your situation you
could sit (or poll if you prefer) in select() on your network and serial
connections in your mail rendering loop…

I have a network library to do just that if you don’t want to write one
yourself (fully non-blocking ofcourse)

Lund

Gib Bogle wrote:> On a Linux box, I’d like to use SDL (to host OpenGL) in a program I’m

developing, preferably with threads, but some snippets I’ve seen
indicate that this might create some limitations. I’d like to have 3
threads, the master handling TCP/IP communications and decoding
instructions, one doing the graphics (SDL/OpenGL), and the third doing
some other serial communications stuff. There will be no mouse or
keyboard attached to this machine. I’d be grateful for advice about the
feasibility or otherwise of this plan. If the threads would create a
problem with SDL, would I be better off with GLUT?

thanks
Gib


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


Jorgen “Lord” Lundman <@Jorgen_Lundman>
Technology Manager, Unix Administrator
Phone: +44 (0)20-86591860 Mobile: +44 (0)79-58642918
Pager: 07958642918 at one2one.net
"Rare is the person who can weigh the faults of others
without putting his thumb on the scales": Byron J. Langenfeld

where exactly is the problem with using graphics and threads? several times i heared
SDL wouldn’t be thread-safe, but i don’t know what exactly this does mean.
probably, the worst thing is i haven’t encountered any problem with that…
are there nasty race-conditions still waiting for me?

It’s possible. It depends on the platform and driver. For example, in
Windows only the main thread in an application gets event messages, and
SDL responds to some of those messages by performing graphics operations.
In addition, by default DirectX isn’t thread-safe. You can turn on a
flag to enable thread-safe graphics access in the DirectX drivers, but
it slows down performance as it has to take critical sections more often.
On X11, there is a single graphics display connection, and if you make
graphics calls from more than one thread, it can corrupt the X11 protocol
queue (which is the cause of the X11 ASYNC errors).

So, in general, unless you really know what you’re doing, it’s better to
do all of your graphics calls from the main thread.

-Sam Lantinga, Software Engineer, Blizzard Entertainment

?where exactly is the problem with using graphics and threads? several times i
heared
?SDL wouldn’t be thread-safe, but i don’t know what exactly this does mean.
?probably, the worst thing is i haven’t encountered any problem with that…
?are there nasty race-conditions still waiting for me?

It’s possible. ?It depends on the platform and driver. ?For example, in
Windows only the main thread in an application gets event messages, and
SDL responds to some of those messages by performing graphics operations.
In addition, by default DirectX isn’t thread-safe. ?You can turn on a
flag to enable thread-safe graphics access in the DirectX drivers, but
it slows down performance as it has to take critical sections more often.
On X11, there is a single graphics display connection, and if you make
graphics calls from more than one thread, it can corrupt the X11 protocol
queue (which is the cause of the X11 ASYNC errors).

So, in general, unless you really know what you’re doing, it’s better to
do all of your graphics calls from the main thread.

???-Sam Lantinga, Software Engineer, Blizzard Entertainment

thank you sam, that cleared my mind up :wink:

l8ers,
Tolga.________________________________________________________________
Keine verlorenen Lotto-Quittungen, keine vergessenen Gewinne mehr!
Beim WEB.DE Lottoservice: http://tippen2.web.de/?x=13

Yes, I encountered all these “features” when developing libMgTk 2.0. Since
libMgTk has it’s own special event handlers, it runs these event handlers in
a second thread. Originally, I had the GUI thread running in a second
thread. This works fine under BeOS, even Linux to an extent (till the X11
protocol corrupted :D), but not under Windows, so I had to do some
restructuring :slight_smile:

----Original Message Follows----From: slouken@libsdl.org (slouken)
Reply-To: sdl at libsdl.org
To: sdl at libsdl.org
Subject: Re: [SDL] SDL and threads, advice please
Date: Mon, 15 Apr 2002 10:50:05 -0700
Received: from [66.224.24.122] by hotmail.com (3.2) with ESMTP id
MHotMailBE845E9100944136E82442E0187AAA1B0; Mon, 15 Apr 2002 10:53:27 -0700
Received: from localhost.localdomain ([127.0.0.1]
helo=twomix.devolution.com)by twomix.devolution.com with esmtp (Exim 3.22
#2)id 16xAd7-0006Ph-00; Mon, 15 Apr 2002 10:51:05 -0700
Received: from slouken by twomix.devolution.com with local (Exim 3.22 #2)id
16xAc9-0006PD-00for sdl at libsdl.org; Mon, 15 Apr 2002 10:50:05 -0700

From sdl-admin at libsdl.org Mon, 15 Apr 2002 10:54:27 -0700
X-Mailer: My Mailer 1.5
Message-Id:
Sender: sdl-admin at libsdl.org
Errors-To: sdl-admin at libsdl.org
X-BeenThere: sdl at libsdl.org
X-Mailman-Version: 2.0.5
Precedence: bulk
List-Help: <mailto:sdl-request at libsdl.org?subject=help>
List-Post: <mailto:sdl at libsdl.org>
List-Subscribe:
http://www.libsdl.org/mailman/listinfo/sdl,<mailto:sdl-request at libsdl.org?subject=subscribe>
List-Id: A list for developers using the SDL library. (includes
SDL-announce) <sdl.libsdl.org>
List-Unsubscribe:
http://www.libsdl.org/mailman/listinfo/sdl,<mailto:sdl-request at libsdl.org?subject=unsubscribe>
List-Archive: http://www.libsdl.org/pipermail/sdl/
X-Original-Date: Mon, 15 Apr 2002 10:50:05 -0700

where exactly is the problem with using graphics and threads? several
times i heared
SDL wouldn’t be thread-safe, but i don’t know what exactly this does
mean.
probably, the worst thing is i haven’t encountered any problem with
that…
are there nasty race-conditions still waiting for me?

It’s possible. It depends on the platform and driver. For example, in
Windows only the main thread in an application gets event messages, and
SDL responds to some of those messages by performing graphics operations.
In addition, by default DirectX isn’t thread-safe. You can turn on a
flag to enable thread-safe graphics access in the DirectX drivers, but
it slows down performance as it has to take critical sections more often.
On X11, there is a single graphics display connection, and if you make
graphics calls from more than one thread, it can corrupt the X11 protocol
queue (which is the cause of the X11 ASYNC errors).

So, in general, unless you really know what you’re doing, it’s better to
do all of your graphics calls from the main thread.

-Sam Lantinga, Software Engineer, Blizzard Entertainment

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

???V?I?T?A?L?I?Y???


Join the world?s largest e-mail service with MSN Hotmail.
http://www.hotmail.com

Many thanks to Sam et al (al who?) for helpful info on threads in SDL.
It is no hardship to restrict graphics calls to a single thread - I
would do it like that anyway. Your second comment didn’t repeat the
"main execution thread" recommendation, Sam. Is that just a matter of
keeping it simple to limit stupidity, or is there a more specific
reason? I can’t think off-hand of a reason not to make the graphics
thread the main one, but such a reason might crop up when I understand
what I’m doing better. (In my case Linux is definitely the only OS
that’ll be used.)

thanks
Gib

Many thanks to Sam et al (al who?) for helpful info on threads in SDL.
It is no hardship to restrict graphics calls to a single thread - I
would do it like that anyway. Your second comment didn’t repeat the
"main execution thread" recommendation, Sam. Is that just a matter of
keeping it simple to limit stupidity, or is there a more specific
reason? I can’t think off-hand of a reason not to make the graphics
thread the main one, but such a reason might crop up when I understand
what I’m doing better. (In my case Linux is definitely the only OS
that’ll be used.)

That the windows event system practically requires it comes to mind. :slight_smile:

See ya,
-Sam Lantinga, Software Engineer, Blizzard Entertainment