Standards / Extensions | C or C++ | Dependencies |
---|---|---|
Language Environment | both | None |
#include <stdio.h>
#include <stdio_ext.h>
int __fsetlocking(FILE *stream, int type);
The __fsetlocking() function allows the type of locking on an open stream to be controlled or queried by the application.
If type is FSETLOCKING_INTERNAL, subsequent stdio functions perform implicit locking around every operation on the given stream. This is the default system behavior.
If type is FSETLOCKING_BYCALLER, subsequent stdio functions assume that the caller is responsible for maintaining the integrity of the stream in the face of access by multiple threads. If only one thread is accessing the stream, nothing further needs to be done. If multiple threads are accessing the stream, you can use the flockfile(), funlockfile(), and ftrylockfile() functions to provide the appropriate serialization.
If type is FSETLOCKING_QUERY, the __fsetlocking() function returns the current locking type of the stream without changing it.
The __fsetlocking() function returns the locking type in effect before the call to __fsetlocking().
The __fsetlocking() function returns -1 if the stream or the locking type is not valid and sets errno to nonzero.
/* CELEBF85
This example sets LOCKING_TYPE as locking type to a stream.
It checks that the fopen() function is successful and that
locking type is setted to the stream and queried from the stream.
*/
#include <stdio.h>
#include <stdio_ext.h>
int main(void)
{
FILE *stream;
int locktype;
int nwrite;
int buflen;
char buf[5]="1234";
if((stream = fopen("myfile.dat", "w+b")) != NULL )
{
locktype = __fsetlocking(stream,FSETLOCKING_BYCALLER);
locktype = __fsetlocking(stream,FSETLOCKING_QUERY);
if(locktype == FSETLOCKING_BYCALLER){
printf("__fsetlocking succeeds! \
Lock type is FSETLOCKING_BYCALLER\n");
}
buflen = strlen(buf);
flockfile(stream);
nwrite = fwrite(buf, 1, buflen, stream);
if(nwrite < buflen){
printf("fwrite did not return expected number of bytes\n");
}
funlockfile(stream);
}
return(0);
}
__fsetlocking succeeds! Lock type is FSETLOCKING_BYCALLER