_lock_file

Verrouille un objet d' FILE pour garantir la sécurité pour les threads accédant à l'objet d' FILE simultanément.

void _lock_file(
   FILE* file
);

Paramètres

  • file
    handle de fichier.

Notes

la fonction d' _lock_file verrouille l'objet d' FILE spécifié par file.le fichier sous-jacent n'est pas verrouillé par _lock_file.Utilisez _unlock_file pour libérer le verrou sur le fichier.Les appels à _lock_file et l' _unlock_file doivent être appariés dans un thread.

Configuration requise

routine

en-tête requis

_lock_file

<stdio.h>

Pour plus d'informations de compatibilité, consultez compatibilité dans l'introduction.

Exemple

// 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 );
}
  
  
  

Équivalent .NET Framework

System : : E/S : : FileStream : : Verrouillage

Voir aussi

Référence

Gestion de fichiers

_create, _wcreat

_open, _wopen

_unlock_file