InterlockedBitTestAndSet function (Windows)

Switch View :
ScriptFree
InterlockedBitTestAndSet function

Applies to: desktop apps only

Tests the specified bit of the specified LONG value and sets it to 1. The operation is atomic.

Syntax

BOOLEAN WINAPI InterlockedBitTestAndSet(
  __in  LONG volatile *Base,
  __in  LONG Bit
);

Parameters

Base [in]

A pointer to a variable.

Bit [in]

The bit position to be tested.

Return value

The value of the specified bit.

Remarks

The interlocked functions provide a simple mechanism for synchronizing access to a variable that is shared by multiple threads. This function is atomic with respect to calls to other interlocked functions.

This function is implemented using a compiler intrinsic where possible. For more information, see the WinBase.h header file and _interlockedbittestandset.

This function generates a full memory barrier (or fence) to ensure that memory operations are completed in order.

Requirements

Header

Winnt.h (include Windows.h)

See also

Interlocked Variable Access
InterlockedBitTestAndReset
InterlockedBitTestAndReset64
InterlockedBitTestAndSet64
Synchronization Functions

 

 

Send comments about this topic to Microsoft

Build date: 3/7/2012

Community Content

Ronald Landheer-Cieslak
Valid values for Bit
Looking the function (macro, actually) up in winbase.h answers most of the questions here: Bit can be any value from 0 to 31 (sizeof(LONG) * 8 - 1), which is "converted" to a LONG value by bit-shifting (so 0 becomes 1, 1 becomes 2, etc.). That value is then ORed with *Base. For the ORing, it uses InterlockedOr, which returns the previous value, which is ANDed with the "converted" value - so the function works as expected in that sense. So it basically does a (*Base |= temp) & temp atomically.

HTH

Alex.Covecube
Memory corruption
This function started causing memory corruption. This is obviously not properly documented.

BobbyMarinov
another badly documented API
Which value is returned? $0- I assume it was the value before setting it, otherwise this API is useless.$0 $0What is the valid range of 'Bit'?$0 $0- 0 to 31, i presume?$0 $0- what about values of 32 to 2147483647 (INT_MAX)?$0 $0$0 $0