Freigeben über


_lock_file

Sperrt ein FILE-Objekt, um die Konsistenz für die Threads sicherzustellen, die gleichzeitig auf das FILE-Objekt zugreifen.

void _lock_file(
   FILE* file
);

Parameter

  • file
    Dateihandle.

Hinweise

Die Funktion _lock_file sperrt das FILE-Objekt, das von file angegeben wird. Die zugrunde liegende Datei wird von _lock_file nicht gesperrt. Verwenden Sie _unlock_file, um die Sperre für die Datei freigeben. Aufrufe von _lock_file und _unlock_file müssen in einem Thread gefunden werden.

Anforderungen

Routine

Erforderlicher Header

_lock_file

<stdio.h>

Weitere Informationen zur Kompatibilität finden Sie unter Kompatibilität in der Einführung.

Beispiel

// crt_lock_file.c
// This example creates multiple threads that write to standard output
// concurrently, first with _file_lock, then without.

#include <stdio.h>
#include <process.h>// _beginthread
#include <windows.h>// HANDLE

void Task_locked( void* str )
{
    for( int i=0; i<1000; ++i )
    {
        _lock_file( stdout );
        for( char* cp = (char*)str; *cp; ++cp )
        {
            _fputc_nolock( *cp, stdout );
        }
        _unlock_file( stdout );
    }
}

void Task_unlocked( void* str )
{
    for( int i=0; i<1000; ++i )
    {
        for( char* cp = (char*)str; *cp; ++cp )
        {
            fputc( *cp, stdout );
        }
    }
}

int main()
{
    HANDLE h[3];
    h[0] = (HANDLE)_beginthread( &Task_locked, 0, "First\n" );
    h[1] = (HANDLE)_beginthread( &Task_locked, 0, "Second\n" );
    h[2] = (HANDLE)_beginthread( &Task_locked, 0, "Third\n" );

    WaitForMultipleObjects( 3, h, true, INFINITE );

    h[0] = (HANDLE)_beginthread( &Task_unlocked, 0, "First\n" );
    h[1] = (HANDLE)_beginthread( &Task_unlocked, 0, "Second\n" );
    h[2] = (HANDLE)_beginthread( &Task_unlocked, 0, "Third\n" );

    WaitForMultipleObjects( 3, h, true, INFINITE );
}
  

.NET Framework-Entsprechung

System::IO::FileStream::Lock

Siehe auch

Referenz

Dateibehandlung

_creat, _wcreat

_open, _wopen

_unlock_file