Name

flockfile, ftrylockfile, funlockfile
- lock FILE for stdio

Library

libc.lib

Synopsis

  #include <stdio.h>
  void flockfile (FILE *stream);
  int ftrylockfile (FILE *stream);
  void funlockfile (FILE *stream);

Return values

The flockfile and funlockfile functions return no value.

The ftrylockfile function returns zero if the stream was successfully locked, non-zero otherwise.


Detailed description

These functions provide explicit application-level locking of stdio streams. They can be used to avoid output from multiple threads being interspersed, input being dispersed among multiple readers, and to avoid the overhead of locking the stream for each operation.

The flockfile function acquires an exclusive lock on the specified stream. If another thread has already locked the stream, flockfile will block until the lock is released.

The ftrylockfile function is a non-blocking version of flockfile; if the lock cannot be acquired immediately, ftrylockfile returns non-zero instead of blocking.

The funlockfile function releases the lock on a stream acquired by an earlier call to flockfile or ftrylockfile.

These functions behave as if there is a lock count associated with each stream. Each time flockfile is called on the stream, the count is incremented, and each time funlockfile is called on the stream, the count is decremented. The lock is only actually released when the count reaches zero.


Examples

#include <stdio.h>
#include <unistd.h>
#include <pthread.h> //link to the lib -libpthread
 
void* somefun(void* args)
{
FILE *fp = (FILE *)args;
printf("in thr 2\n");
flockfile(fp);
printf("aquired lock!\n");
fputc('a', fp); //fputc_unlocked() is more relevant
printf("after a from thr 2\n");
sleep(3);
printf("after sleep from thr 2\n");
fputc('b', fp);
printf("after b from thr 2\n");
fputc('c', fp);
printf("after c from thr 2\n");
funlockfile(fp);
fclose(fp);
}
int main()
{
pthread_t obj;
FILE *fp = fopen("c:\\chk.txt", "w");
if(fp)
{
        flockfile(fp);
        fputc('x', fp); //fputc_unlocked() is more relevant
        printf("after x from thr 1\n");
        sleep(5);
        printf("after sleep from thr 1\n");
        pthread_create(&obj, NULL, somefun, fp);
        printf("after calling thr 2 from thr 1\n");
        fputc('y', fp);
        printf("after y from thr 1\n");
        fputc('z', fp);
        printf("after z from thr 1\n");
        funlockfile(fp);
        printf("gave up lock in thr 1\n");
}
pthread_exit((void *)0);
}

         

Output

after x from thr 1
after sleep from thr 1
in thr 2
after calling thr 2 from thr 1
after y from thr 1
after z from thr 1
gave up lock in thr 1
acquired lock!
after a from thr 2
after sleep from thr 2
after b from thr 2
after c from thr 2
  
Note: The printing takes quite some time and hence the
output may not look exactly like the above one.
(try printing to the files if you are very particular)
 

         

#include <stdio.h>
#include <unistd.h>
#include <pthread.h> //link to lib -libpthread
#include <errno.h>
 
void* somefun(void* args)
{
 
FILE *fp = (FILE *)args;
 
printf("in thr 2\n");
 
int i = ftrylockfile(fp);
if(i == 0)
{
        printf("aquired lock!\n");
        fputc('a', fp);
        printf("after a from thr 2\n");
        sleep(3);
        printf("after sleep from thr 2\n");
        fputc('b', fp);
        printf("after b from thr 2\n");
        fputc('c', fp);
        printf("after c from thr 2\n");
        funlockfile(fp);
        printf("gave up lock in thr 2\n");
}
else
        printf("couldn't aquire lock\n");
}
int main()
{
pthread_t obj;
 
FILE *fp = fopen("c:\\chk.txt", "w");
 
if(fp)
{
        flockfile(fp);
        fputc('x', fp);
        printf("after x from thr 1\n");
        sleep(5);
        printf("after sleep from thr 1\n");
        pthread_create(&obj, NULL, somefun, fp);
        printf("after calling thr 2 from thr 1\n");
        fputc('y', fp);
        printf("after y from thr 1\n");
        fputc('z', fp);
        printf("after z from thr 1\n");
        funlockfile(fp);
        printf("gave up lock in thr 1\n");
        sleep(5);
        fclose(fp);
}
pthread_exit((void *)0);
}

         

Output

after x from thr 1
after sleep from thr 1
in thr 2
couldn’t acquire lock
after calling thr 2 from thr 1
after y from thr 1
after z from thr 1
gave up lock in thr 1
  
Note: The printing takes quite some time and hence the
output may not look exactly like the above one.
(try printing to the files if you are very particular)
  

         

See also

getc_unlocked, putc_unlocked

Feedback

For additional information or queries on this page send feedback

© 2005-2007 Nokia

Top