12e5b6d6dSopenharmony_ci---
22e5b6d6dSopenharmony_cilayout: default
32e5b6d6dSopenharmony_cititle: Synchronization
42e5b6d6dSopenharmony_cinav_order: 2
52e5b6d6dSopenharmony_ciparent: Contributors
62e5b6d6dSopenharmony_ci---
72e5b6d6dSopenharmony_ci<!--
82e5b6d6dSopenharmony_ci© 2020 and later: Unicode, Inc. and others.
92e5b6d6dSopenharmony_ciLicense & terms of use: http://www.unicode.org/copyright.html
102e5b6d6dSopenharmony_ci-->
112e5b6d6dSopenharmony_ci
122e5b6d6dSopenharmony_ci# Synchronization Issues
132e5b6d6dSopenharmony_ci{: .no_toc }
142e5b6d6dSopenharmony_ci
152e5b6d6dSopenharmony_ci## Contents
162e5b6d6dSopenharmony_ci{: .no_toc .text-delta }
172e5b6d6dSopenharmony_ci
182e5b6d6dSopenharmony_ci1. TOC
192e5b6d6dSopenharmony_ci{:toc}
202e5b6d6dSopenharmony_ci
212e5b6d6dSopenharmony_ci---
222e5b6d6dSopenharmony_ci
232e5b6d6dSopenharmony_ci## Overview
242e5b6d6dSopenharmony_ci
252e5b6d6dSopenharmony_ciICU is designed for use in multi-threaded environments. Guidelines for
262e5b6d6dSopenharmony_cidevelopers using ICU are in the [ICU Design](../../design.md) section of the
272e5b6d6dSopenharmony_ciuser guide.
282e5b6d6dSopenharmony_ci
292e5b6d6dSopenharmony_ciWithin the ICU implementation, access to shared or global data sometimes must be
302e5b6d6dSopenharmony_ciprotected in order to provide the threading model promised by the ICU design.
312e5b6d6dSopenharmony_ciThe information on this page is intended for developers of ICU library code
322e5b6d6dSopenharmony_ciitself.
332e5b6d6dSopenharmony_ci
342e5b6d6dSopenharmony_ciICU4J uses normal JDK synchronization services.
352e5b6d6dSopenharmony_ci
362e5b6d6dSopenharmony_ciICU4C faces a more difficult problem, as there is no standard, fully portable
372e5b6d6dSopenharmony_ciset of C or C++ synchronization primitives. Internally, ICU4C provides a small
382e5b6d6dSopenharmony_ciset of synchronization operations, and requires that all synchronization needed
392e5b6d6dSopenharmony_ciwithin the ICU library code be implemented using them.
402e5b6d6dSopenharmony_ci
412e5b6d6dSopenharmony_ciThe ICU4C synchronization primitives are for internal use only; they are not
422e5b6d6dSopenharmony_ciexported as API to normal users of ICU.
432e5b6d6dSopenharmony_ci
442e5b6d6dSopenharmony_ciICU provides implementations of its synchronization functions for Windows, POSIX
452e5b6d6dSopenharmony_ciand C++11 platforms, and provides a build-time interface to allow [custom
462e5b6d6dSopenharmony_ciimplementations](custom.md) for other platforms.
472e5b6d6dSopenharmony_ci
482e5b6d6dSopenharmony_ci## ICU4C Synchronization Primitives
492e5b6d6dSopenharmony_ci
502e5b6d6dSopenharmony_ciThe functions and types listed below are intended for use throughout the ICU
512e5b6d6dSopenharmony_cilibrary code, where ever synchronization is required. They are defined in the
522e5b6d6dSopenharmony_ciinternal header
532e5b6d6dSopenharmony_ci[umutex.h](https://github.com/unicode-org/icu/blob/main/icu4c/source/common/umutex.h).
542e5b6d6dSopenharmony_ci
552e5b6d6dSopenharmony_ciAll synchronization within ICU4C implementation code must use these, and avoid
562e5b6d6dSopenharmony_cidirect use of functions provided by a particular operating system or compiler.
572e5b6d6dSopenharmony_ci
582e5b6d6dSopenharmony_ciFor examples of use, search the ICU library code.
592e5b6d6dSopenharmony_ci
602e5b6d6dSopenharmony_ci**Low Level Atomics**
612e5b6d6dSopenharmony_ci
622e5b6d6dSopenharmony_ci| Type/Function                            | Description                                                     |
632e5b6d6dSopenharmony_ci|------------------------------------------|-----------------------------------------------------------------|
642e5b6d6dSopenharmony_ci| `typedef u_atomic_int32_t`               | A 32 bit integer type for use with low level atomic operations. |
652e5b6d6dSopenharmony_ci| `umtx_atomic_inc(u_atomic_int32_t &var)` |                                                                 |
662e5b6d6dSopenharmony_ci| `umtx_atomic_dec(u_atomic_int32_t &var)` |                                                                 |
672e5b6d6dSopenharmony_ci
682e5b6d6dSopenharmony_ci**Mutexes**
692e5b6d6dSopenharmony_ci
702e5b6d6dSopenharmony_ci| Type/Function                | Description                                                           |
712e5b6d6dSopenharmony_ci|------------------------------|-----------------------------------------------------------------------|
722e5b6d6dSopenharmony_ci| `struct UMutex`              | An ICU mutex. All instances must be `static`.                         |
732e5b6d6dSopenharmony_ci| `U_MUTEX_INITIALIZER`        | A C style initializer for a `UMutex`.                                 |
742e5b6d6dSopenharmony_ci| `umtx_lock(UMutex *mutex)`   | Lock a mutex.                                                         |
752e5b6d6dSopenharmony_ci| `umtx_unlock(UMutex* mutex)` | Unlock a mutex.                                                       |
762e5b6d6dSopenharmony_ci| `class Mutex`                | C++ Mutex wrapper with automatic lock & unlock. See header `mutex.h.` |
772e5b6d6dSopenharmony_ci
782e5b6d6dSopenharmony_ci**One Time Initialization**
792e5b6d6dSopenharmony_ci
802e5b6d6dSopenharmony_ci| Type/Function                   | Description                                                                             |
812e5b6d6dSopenharmony_ci|---------------------------------|-----------------------------------------------------------------------------------------|
822e5b6d6dSopenharmony_ci| `struct UInitOnce`              | Provides an efficient facility for one-time initialization of static or global objects. |
832e5b6d6dSopenharmony_ci| `umtx_initOnce(UInitOnce, ...)` | A family of initialization functions.                                                   |
842e5b6d6dSopenharmony_ci
852e5b6d6dSopenharmony_ciAll of these functions are for internal ICU implementation use only. They are
862e5b6d6dSopenharmony_cinot exported, and not intended for external use.
87