This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

File locking with gcc 3


How does one do Posix file locking with gcc3 / libstdc++-v3?
The program is written in C++, and I'm trying to compile it
under Solaris 8. It is a simple booking system where the program
locks a booking data file, reads the last request number from it,
then writes a new booking with an incremented request number before
releasing the lock.

The following fragment used to compile under g++ 2.95...

-----------------------
#define GCC3  1

#if GCC3              // mods for gcc 3
#include <iostream>
#include <fstream>
#include <fcntl.h>
#include <cstdio>
#include <cctype>
using namespace std;
#else
#include <fstream.h>
#include <fcntl.h>                      // for file locking
#include <stdio.h>
#include <ctype.h>
#endif

int JobRequest::getlock()
{
        struct flock    lock;

    fd = jobfile.rdbuf()->fd();
    lock.l_type = F_WRLCK;
    lock.l_start = 0;
    lock.l_whence = SEEK_SET;
    lock.l_len = 0;

    return( fcntl(fd, F_SETLK, &lock) );
}
-----------------------

....but with g++ 3.0.2 I get
request.cpp: In member function `int JobRequest::getlock()':
request.cpp:70: no matching function for call to `std::basic_filebuf<char, 
   std::char_traits<char> >::fd()'

I've also tried replacing the body of the function with the line

  return( fcntl(fd, F_SETLKW, file_lock(F_WRLCK, SEEK_SET)));

but g++  then gives the error...
  request.cpp:79: `file_lock' undeclared (first use this function)

...and file_lock() does not appear to be defined in any of the system
include files.

Perhaps there is another method of locking files?

thanks,

Chris Blake



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]