InterlockedCompareExchange128

转载 2012年03月31日 14:31:21
InterlockedCompareExchange128
Visual Studio 2010 - Visual C++
_InterlockedCompareExchange128

Microsoft Specific

Performs a 128-bit interlocked compare and exchange.

unsigned char _InterlockedCompareExchange128(
   __int64 volatile * Destination,
   __int64 ExchangeHigh,
   __int64 ExchangeLow,
   __int64 * ComparandResult
);
Parameters
[in, out] Destination

Pointer to the destination, which is an array of two 64-bit integers considered as a 128-bit field. The destination data must be 16-byte aligned to avoid a general protection fault.

[in] ExchangeHigh

A 64-bit integer that may be exchanged with the high part of the destination.

[in] ExchangeLow

A 64-bit integer that may be exchanged with the low part of the destination.

[in, out] ComparandResult

Pointer to an array of two 64-bit integers (considered as a 128-bit field) to compare with the destination. On output, this is overwritten with the original value of the destination.

Return Value

1 if the 128-bit comparand equals the original value of the destination. ExchangeHigh and ExchangeLow overwrite the 128-bit destination.

0 if the comparand does not equal the original value of the destination. The value of the destination is unchanged and the value of the comparand is overwritten with the value of the destination.

Requirements

Intrinsic

Architecture

_InterlockedCompareExchange128

x64

Header file <intrin.h>

Remarks

This intrinsic generates the cmpxchg16b instruction (with the lock prefix) to perform a 128-bit locked compare and exchange. Early versions of AMD 64-bit hardware do not support this instruction. To check for hardware support for the cmpxchg16b instruction, call the __cpuid intrinsic with InfoType=0x00000001 (standard function 1). Bit 13 of CPUInfo[2] (ECX) is 1 if the instruction is supported.

NoteNote

The value of ComparandResult is always overwritten. After the lock instruction, this intrinsic immediately copies the initial value of Destination to ComparandResult. For this reason, ComparandResult and Destination should point to separate memory locations to avoid unexpected behavior.

Although you can use _InterlockedCompareExchange128 for low-level thread synchronization, you do not need to synchronize over 128 bits if you can use smaller synchronization functions (such as the other _InterlockedCompareExchange intrinsics) instead. Use _InterlockedCompareExchange128 if you want atomic access to a 128-bit value in memory.

If you run code that uses this intrinsic on hardware that does not support the cmpxchg16b instruction, the results are unpredictable.

This routine is available only as an intrinsic.

Example

This example uses _InterlockedCompareExchange128 to replace the high word of an array of two 64-bit integers with the sum of its high and low words and to increment the low word. The access to the BigInt.Int array is atomic, but this example uses a single thread and ignores the locking for simplicity.

// cmpxchg16b.c
// processor: x64
// compile with: /EHsc /O2
#include <stdio.h>
#include <intrin.h>

typedef struct _LARGE_INTEGER_128 {
    __int64 Int[2];
} LARGE_INTEGER_128, *PLARGE_INTEGER_128;

volatile LARGE_INTEGER_128 BigInt;

// This AtomicOp() function atomically performs:
//   BigInt.Int[1] += BigInt.Int[0]
//   BigInt.Int[0] += 1
void AtomicOp ()
{
    LARGE_INTEGER_128 Comparand;
    Comparand.Int[0] = BigInt.Int[0];
    Comparand.Int[1] = BigInt.Int[1];
    do {
        ; // nothing
    } while (_InterlockedCompareExchange128(BigInt.Int,
                                            Comparand.Int[0] + Comparand.Int[1],
                                            Comparand.Int[0] + 1,
                                            Comparand.Int) == 0);
}

// In a real application, several threads contend for the value
// of BigInt.
// Here we focus on the compare and exchange for simplicity.
int main(void)
{
   BigInt.Int[1] = 23;
   BigInt.Int[0] = 11;
   AtomicOp();
   printf("BigInt.Int[1] = %d, BigInt.Int[0] = %d\n",
      BigInt.Int[1],BigInt.Int[0]);
}
BigInt.Int[1] = 34, BigInt.Int[0] = 12
END Microsoft Specific

Copyright 2007 by Advanced Micro Devices, Inc. All rights reserved. Reproduced with permission from Advanced Micro Devices, Inc.

相关文章推荐

三种条码code39和code93以及code128在应用中的比较

  与经常在商品上见到的ENA 13条码不同,code39, code93和 code128常用于企业自定义编码的应用。  从易用性来说,code39显示是最容易的,其编码只需要对原字符串前后加上符号...

解决ora-01652无法通过128(在temp表空间中)扩展temp段的过程

解决ora-01652无法通过128(在temp表空间中)扩展temp段的过程 来源:http://www.cnblogs.com/rootq/archive/2009/02/05/1384539....
  • ytfy12
  • ytfy12
  • 2013年06月20日 18:00
  • 2693

C语言实现AES-128 CMAC算法

美国国家标准与技术研究院NIST推荐了一种CMAC计算方式,可以避免CBC-MAC带来的缺点,编号为800-3B,文档可以从其官网上下载。该算法通过MAC密钥生成k1和k2两个子密钥,并规定了数据位填...

AES128比特10轮密钥编排方案

这是课后的一道题目:3.5 假如有128比特的AES密钥,用十六进制表示为: 2B7E151628AED2A6ABF7158809CF4F3C 由上述种子密钥构造一个完整的密钥编排方案。 AES是Ri...

一道面试题关于Integer的缓存范围(-128~127)所引起的一系列问题记录

JVM会自动维护八种基本类型的常量池,int常量池中初始化-128~127的范围,所以当为Integer i=127时,在自动装箱过程中是取自常量池中的数值,而当Integer i=128时,128不...
  • BeauXie
  • BeauXie
  • 2016年11月02日 22:31
  • 758

【BLE-CC2640】CC2640之使用自定义128bit的UUID

本文以SimpleBLEPeripheral工程为例,介绍如何在工程中使用自定义128bit的UUID

char能表示(-128~127)

char 的取值范围是 -128 ~127 注:数0的补码表示是唯一的: +0的补码=+0的反码=+0的原码=00000000 -0的补码=11111111+1=00000000(mod 2的8...

一维条形码 code128 的全面介绍

0:code128,编码格式: Code128A字符集 包括大写字母、数字、常用标点符号和一些控制符。 Code128B字符集 包括大小写字母、数字、常用标点符号。 Code128C...
  • Rodulf
  • Rodulf
  • 2016年04月28日 19:07
  • 3938

128.泡泡窗功能的实现

泡泡窗有卸载,打开,分享以及显示详细信息等功能,显示详细信息需要读取配置的权限。 泡泡窗的布局文件item_popup.xml ...
  • augfun
  • augfun
  • 2017年04月17日 06:12
  • 493

ICCBBA的ISBT 128编码标准 检验码在codesoft的实现

ISBT 128来源于code 128条码,在条码系统中也可以直接用code 128或其他一维条码代替,但最终结果只要符合ISBT128的内容即可。如: 其中,,前五位字符(90003)[fac...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:InterlockedCompareExchange128
举报原因:
原因补充:

(最多只允许输入30个字)