# SDL with linuxthreads

**URL:** <https://discourse.libsdl.org/t/sdl-with-linuxthreads/14775>\
**Category:** SDL Development\
**Created:** [August 6, 2007, 10:49pm UTC](https://discourse.libsdl.org/t/sdl-with-linuxthreads/14775 "2007-08-06T22:49:21Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sam](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/s/5f9b8f/32.png) [@Sam](https://discourse.libsdl.org/u/Sam)\
**Post date:** [August 6, 2007, 10:49pm UTC](https://discourse.libsdl.org/t/sdl-with-linuxthreads/14775/1 "2007-08-06T22:49:21Z")

</div>

Is there any issues with using SDL and linuxthreads instead of NPTL (Native  
Posix Thread Library)?

## This is my test system kernel version and glibc version:— Linux version 2.6.18.1

GNU C Library stable release version 2.3.2, by Roland McGrath et al.  
Compiled by GNU CC version 3.4.2 (release) (CodeSourcery ARM Q3D 2004).  
Compiled on a Linux 2.6.0-test7 system on 2005-11-03.  
Available extensions:  
GNU libio by Per Bothner  
crypt add-on version 2.1 by Michael Glad and others  
linuxthreads-0.10 by Xavier Leroy  
BIND-8.2.3-T5B  
libthread\_db work sponsored by Alpha Processor Inc  
NIS(YP)/NIS+ NSS modules 0.19 by Thorsten Kukuk

======================================  
In my application I am using SDL 1.2.11 with Paragui.

I found this weird behaviour:

I create a new “process” using fork(), then run this command:

while (true) {  
system(“ls”);  
system(“df”);  
system(“free”);  
system(“ls -l”);  
sleep(3);  
}

Everything runs fine as usual. These system commands will just run on the  
background.

Now, I create a new “thread” using pthread, then run this command:

while (true) {  
system(“ls”);  
system(“df”);  
system(“free”);  
system(“ls -l”);  
sleep(3);  
}

Application will CRASH within 30 minutes. Sometimes within 5 minutes. It will  
crash with exit code 0.

Anybody knows why this is happening?

Thanks.

---

<div class="post-metadata">

**Author:** ![icculus](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/icculus/32/81_2.png) [@icculus](https://discourse.libsdl.org/u/icculus)\
**Post date:** [August 6, 2007, 11:04pm UTC](https://discourse.libsdl.org/t/sdl-with-linuxthreads/14775/2 "2007-08-06T23:04:23Z")

</div>

> Application will CRASH within 30 minutes. Sometimes within 5 minutes. It will  
> crash with exit code 0.

We’d need to see the backtrace in gdb, or at least the source code when  
you create the thread. I don’t see any reason it should crash there,  
based on the information I have; maybe it’s crashing somewhere else?

–ryan.

---

<div class="post-metadata">

**Author:** ![Sam](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/s/5f9b8f/32.png) [@Sam](https://discourse.libsdl.org/u/Sam)\
**Post date:** [August 7, 2007, 12:11am UTC](https://discourse.libsdl.org/t/sdl-with-linuxthreads/14775/3 "2007-08-07T00:11:23Z")

</div>

Ryan C. Gordon \<icculus [icculus.org](http://icculus.org)\> writes:

> > Application will CRASH within 30 minutes. Sometimes within 5 minutes. It will  
> > crash with exit code 0.
> 
> We’d need to see the backtrace in gdb, or at least the source code when  
> you create the thread. I don’t see any reason it should crash there,  
> based on the information I have; maybe it’s crashing somewhere else?
> 
> –ryan.

Ryan, thanks for the interest in this problem.

# Thread is created like this, pretty straight forward:================================================================== // global static var. static pthread\_t testThread;

# // thread function. static void\* threadFunction(void\* args) { int rVal = 0; sleep(10); while (true) { rVal = system(“df -h”); rVal = system(“ls”); rVal = system(“route -n”); rVal = system(“ifconfig”); rVal = system(“ls -l”); rVal = system(“free”); sleep(3); } pthread\_exit(0); return (void\*)NULL; }

# // this code executed after application is running. pthread\_create(&testThread, NULL, threadFunction, (void\*)NULL);

Yes, I don’t think the thread code is crashing, but it is causing something to  
crash. If I run the exact same “system()” commands from a “forked” process,  
instead of using threads, application will run just fine. Also, when it crashes  
it will exit with code “0”.

I am also using SDL Timers in my application.

Exact same system commands will crash the application when it is run from a  
thread and it will never crash when it is run from a forked process.

So, thought maybe it is related to the thread model I’m using.

… Sam.

---

<div class="post-metadata">

**Author:** ![slouken](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/s/ec9cab/32.png) [@slouken](https://discourse.libsdl.org/u/slouken)\
**Post date:** [August 7, 2007, 12:57am UTC](https://discourse.libsdl.org/t/sdl-with-linuxthreads/14775/4 "2007-08-07T00:57:53Z")

</div>

> So, thought maybe it is related to the thread model I’m using.

Have you tried with a standalone application without SDL?

Have you tried with SDL’s threads?

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

---

<div class="post-metadata">

**Author:** ![Sam](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/s/5f9b8f/32.png) [@Sam](https://discourse.libsdl.org/u/Sam)\
**Post date:** [August 7, 2007, 10:17pm UTC](https://discourse.libsdl.org/t/sdl-with-linuxthreads/14775/5 "2007-08-07T22:17:12Z")

</div>

Sam Lantinga \<slouken [devolution.com](http://devolution.com)\> writes:

> Have you tried with a standalone application without SDL?

Never crashes on standalone application. You can make system calls all you want  
from both threads or forked processes.

> Have you tried with SDL’s threads?

Yes, I tried it with SDL’s threads. Same thing like pthreads, it will crash  
pretty fast.

Is it possible that signal is interfering with SDL’s timer. I’ll have to test  
more, but I think if I don’t use SDL’s timer it works fine.

But I need to use SDL’s TIMER.

Also, same code never crashes on a Linux system that is using NPTL (Native Posix  
Thread Library).

… Sam.

---

<div class="post-metadata">

**Author:** ![Brian\_Barrett](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/brian_barrett/32/427_2.png) [@Brian\_Barrett](https://discourse.libsdl.org/u/Brian_Barrett)\
**Post date:** [August 8, 2007, 8:10am UTC](https://discourse.libsdl.org/t/sdl-with-linuxthreads/14775/6 "2007-08-08T08:10:21Z")

</div>

Are you sure its not an issue elsewhere in the program? If you omit  
the system() calls, does your program still crash?

SDL\_Timers can run in separate threads. Perhaps you are getting  
synchronisation issues, and/or memory corruption due to improperly  
shielded shared data access.

Can you show the source to a minimal example that demonstrates this?On 07/08/07, Sam wrote:

> Is it possible that signal is interfering with SDL’s timer. I’ll have to test  
> more, but I think if I don’t use SDL’s timer it works fine.
> 
> But I need to use SDL’s TIMER.
> 
> Also, same code never crashes on a Linux system that is using NPTL (Native Posix  
> Thread Library).
> 
> … Sam.

---

<div class="post-metadata">

**Author:** ![Sam](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/s/5f9b8f/32.png) [@Sam](https://discourse.libsdl.org/u/Sam)\
**Post date:** [August 8, 2007, 8:54pm UTC](https://discourse.libsdl.org/t/sdl-with-linuxthreads/14775/7 "2007-08-08T20:54:20Z")

</div>

Brian \<brian.ripoff [gmail.com](http://gmail.com)\> writes:

> Are you sure its not an issue elsewhere in the program? If you omit  
> the system() calls, does your program still crash?

No it does not crash without these system calls. Also, if the systems calls are  
being made from a separate forked process, it will never crash.

> SDL\_Timers can run in separate threads. Perhaps you are getting  
> synchronisation issues, and/or memory corruption due to improperly  
> shielded shared data access.
> 
> Can you show the source to a minimal example that demonstrates this?

Let me try to make a minimal example.

Thanks.

… Sam.
