The OS-level chdir() and getcwd() are not thread-safe. The usual
advice is to ignore them completely in multithreaded programs, in
favor of absolute paths (or relative paths from a fixed location,
never calling chdir()).  This is part of the a reason that Apache2
recommends fastcgi for non-prefork MPMs (since the CGI spec requires
cgi scripts to start with getcwd() set to the directory containing
    Since qqx has to fork a new process anyway, having it chdir(*CWD)
is not a big deal - as long as it's the child, not the parent, that
calls chdir().  But any process with multiple threads should generally
avoid calling chdir() ever.
  All of this is assuming that P6's chdir() just calls the OS routine,
of course.  It could be made fancier and handle the thread-stuff

On 8/18/09, Timothy S. Nelson <> wrote:
>       Ok, here's a fairly significant point posted on IRC.
>   <TimToady> wayland76: the point of using $*CWD would be (and would *have*
> to
> be, given how context vars work) to give each thread its own working
> directory, independent of the process as a whole
>       Now, given that chdir is an OS concept that applies to the process as
> a whole, rather than individual threads, but $*CWD is designed for
> individual
> threads, that pretty much means that it is a really bad idea to have $*CWD
> do
> chdir() for us.
>       Now, having said that, I think the idea of a per-thread CWD is a great
> idea, and I'd be in favour of using $*CWD over whatever getcwd retrns in
> Perl6
> as much as possible.  So I think it would be cool if all relative paths
> specified via p{} were relative to $*CWD, not getcwd().  So something like
> this:
>       chdir("/usr/bin");
>       $*CWD = "/home/username";
>       $path = p{Music};
>       say $path.canonpath();
>       # prints "/home/username/Music"
>       My question is, what else uses chdir/getcwd style paths?  I know
> qqx/$cmd @args[]/
>       ...will pass its current dir to the child.  Maybe we could have Perl6
> do something like this pseudocode "under the hood" when someone does a qqx.
> olddir = getcwd();
> chdir($*CWD);
> exec(qqx string here);
> chdir(olddir);
>       That would mean that $*CWD would effectively act as a per-thread
> working directory.
>       No doubt someone will write in and tell me how bad an idea this is.  I
> look forward to having my ignorance exposed :).
> ---------------------------------------------------------------------
> | Name: Tim Nelson                 | Because the Creator is,        |
> | E-mail:    | I am                           |
> ---------------------------------------------------------------------
> Version 3.12
> GCS d+++ s+: a- C++$ U+++$ P+++$ L+++ E- W+ N+ w--- V-
> PE(+) Y+>++ PGP->+++ R(+) !tv b++ DI++++ D G+ e++>++++ h! y-

Sent from my mobile device

Mark J. Reed <>

Reply via email to