[prev in list] [next in list] [prev in thread] [next in thread] 

List:       apache-modperl
Subject:    Re: Thread-safe & vars scope clarification
From:       Ben RUBSON <ben.rubson () gmail ! com>
Date:       2016-04-07 14:39:15
Message-ID: D3D96E2A-5C8A-4545-ACAB-D7F04A8DAEE2 () gmail ! com
[Download RAW message or body]


>> If you want to try it yourself and report back, I'm sure we'd all be
>> interested in what you find out. The main thing I'm aware of is that
>> copy-on-write works very well for preforking and threads usually can't
>> match it, but maybe your application is different.
>> 
>> Regarding your thread-safety questions, chdir() is global to the process,
>> so not thread-safe. I think the issue with readdir() is that open directory
>> handles don't get copied when threads are spawned, but maybe there's more
>> to it. The special Perl vars are not a problem with threads, since each
>> thread is a separate interpreter.
>> 
>> You can read about the limitations of threads here:
>> http://perldoc.perl.org/threads.html#BUGS-AND-LIMITATIONS
> 
> File handles, directory handles, cwd, uid, euid, gid, egid, signal 
> mask (%SIG): anything defined in the process struct is shared among 
> all threads. For example, the BSD proc sturuct looks like:
> 
> <http://people.freebsd.org/~meganm/data/tutorials/ddwg/ddwg63.html>
> 
> which gives a rough idea of what is in common.
> 
> The only thing you can do with threads that forks make difficult is 
> having multiple streams of execution working on a common data 
> structure in parallel with about-to-block threads returning control
> before they actually do block. Nice for a web server or database, but
> you probably aren't writing an RDBMS in Perl :-)
> 
> By the time your computing problem has percolated up to the level of
> Perl (vs. C) you are probably better off dealing with the work using
> forks (at least on *NIX) in order to avoid all of the locking, memory,
> and pool-manglement issues. 

Perrin, Steven, sorry for my late answer and thank you very much for your clarification and useful links.

So no I'm not writing a RDBMS :) but a filesystem API which of course uses readdir().
So according to your answers, threads sound dangerous in this situation.

This link was also very useful to understand how Apache threads work with mod_perl :
https://perl.apache.org/docs/2.0/user/intro/overview.html#Threads_Support

The difficulty (from my point of view) seems to have the answer to "is my code thread-safe ?".
I did not found an exhaustive list of unsafe functions, and documentation does not help much :
"Other usually problematic functions include readdir(), srand(), etc."

In addition, seems to hava exceptions :
Here http://perldoc.perl.org/threads.html#BUGS-AND-LIMITATION, paragraph "Open directory handles" :
Seems that opendir() (so readdir() ?) could be thread-safe if system has fchdir support.
But how to be sure...

I think I will stay with the classic forked MPM for security reasons :)

Thank you !

Ben

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic