You copied the Doc URL to your clipboard.

__strex intrinsic

The __strex intrinsic lets you use an STREX[size] instruction in your C or C++ code to store data to memory.


The compiler does not guarantee that it will preserve the state of the exclusive monitor. It may generate load and store instructions between the LDREX instruction generated for the __ldrex intrinsic and the STREX instruction generated for the __strex intrinsic. Because memory accesses can clear the exclusive monitor, code using the __ldrex and __strex intrinsics can have unexpected behavior. Where LDREX and STREX instructions are needed, ARM recommends using embedded assembly.


int __strex(unsigned int val, volatile void *ptr)



is the value to be written to memory.


points to the address of the data to be written to in memory. To specify the size of the data to be written, cast the parameter to an appropriate integral type.

Table 10-11 Access widths that the __strex intrinsic supports

Instruction Size of data stored Pointer type
STREXB byte char *
STREXH halfword short *
STREX word int *

Return value

The __strex intrinsic returns:


if the STREX instruction succeeds


if the STREX instruction is locked out.


The compiler does not recognize the __strex intrinsic when compiling for a target that does not support the STREX instruction. The compiler generates either a warning or an error in this case, depending on the source language:

  • In C code: Warning: #223-D: function "__strex" declared implicitly.
  • In C++ code: Error: #20: identifier "__strex" is undefined.

The __strex intrinsic does not support access to doubleword data. The compiler generates an error if you specify an access width that is not supported.


int foo(void)
    int loc=0xff;
    return(!__strex(0x20, (volatile char *)loc));

Compiling this code with the command-line option --cpu=6k produces

||foo|| PROC
    MOV      r0,#0xff
    MOV      r2,#0x20
    STREXB   r1,r2,[r0]
    CMP      r1,#0
    MOVEQ    r0,#1
    MOVNE    r0,#0
    BX       lr

Related information