On 2002-03-27 15:27:06 +0300, Alexander Shalaev wrote:
> 
> Can i use readdir_r() functon with pth threads library, or how can i
> use safely readdir() with pthlib ?

If you're going to keep the dirent structure around any length of time,
you'll need to do a copy of the contents: 

  struct dirent *cpy;

  cpy = (struct dirent *)malloc(sizeof(struct dirent) + strlen(src->d_name)); 
  assert(cpy != NULL);
  *cpy = *src;
  strcpy(cpy->d_name, src->d_name);

If you don't copy, you need to be sure that the code never gets ported
to POSIX threads, and never gets restructured such that any I/O occurs
between the readdir() call and when the value is used (or any other call
that causes Pth to context switch).

  while (src = readdir(dirp)) {
      if ((++cnt % 100) == 0) {
          status("Reading directory ... %d", cnt);  // context switch???
      }
      do_something(src);
  }

This is actually a fairly reasonable set of assumptions, but unless you
need to port to systems that don't support API extensions used by POSIX
threads, I'd go with readdir_r() as the path of least resistance.

-- 
Shane
Carpe Diem
______________________________________________________________________
GNU Portable Threads (Pth)            http://www.gnu.org/software/pth/
User Support Mailing List                            [EMAIL PROTECTED]
Automated List Manager (Majordomo)           [EMAIL PROTECTED]

Reply via email to