[#BloombergLP-bdls-FilesystemUtil-tryLock]
= xref:BloombergLP.adoc[BloombergLP]::xref:BloombergLP/bdls.adoc[bdls]::xref:BloombergLP/bdls/FilesystemUtil.adoc[FilesystemUtil]::tryLock
:relfileprefix: ../../../
:mrdocs:

Acquire a lock for the file with `descriptor` if it is available&period;

== Synopsis

Declared in `&lt;bdls&lowbar;filesystemutil&period;h&gt;`

[source,cpp,subs="verbatim,replacements,macros,-callouts"]
----
static
int
tryLock(
    xref:BloombergLP/bdls/FilesystemUtil/FileDescriptor.adoc[FileDescriptor] descriptor,
    bool lockWriteFlag);
----

== Description

Acquire a lock for the file with the specified `descriptor` if it is currently available&period; If the specified `lockWriteFlag` is true, acquire an exclusive write lock unless another process has any type of lock on the file&period; If `lockWriteFlag` is false, acquire a shared read lock unless a process has a write lock&period; This method will not block&period; Return 0 on success, `k&lowbar;ERROR&lowbar;LOCKING&lowbar;CONFLICT` if the platform reports the lock could not be acquired because another process holds a conflicting lock, and a negative value for any other kind of error&period; Note that this operation locks the indicated file for the current _process_, but the behavior is unspecified (and platform&hyphen;dependent) when either attempting to lock `descriptor` multiple times, or attempting to lock another descriptor referring to the same file, within a single process&period;

== Return Value

0 on success, `k&lowbar;ERROR&lowbar;LOCKING&lowbar;CONFLICT` on a conflicting lock, and a negative value for any other error

== Parameters

[cols="1,4"]
|===
| Name| Description
| *descriptor*
| file descriptor of the file to lock
| *lockWriteFlag*
| `true` for an exclusive write lock&semi; `false` for a shared read lock
|===


[.small]#Created with https://www.mrdocs.com[MrDocs]#
