<!--
title: Centralize synchronization logic
domain: ml-systems
topic: Concurrency
language: Other
source: apache/mxnet
updated: 2021-09-02
url: https://awesomereviewers.com/reviewers/mxnet-centralize-synchronization-logic/
-->

When implementing concurrent operations, especially in heterogeneous computing environments (CPU/GPU), centralize synchronization management in dedicated components rather than using flags scattered throughout the codebase. This approach improves maintainability, reduces the potential for race conditions, and makes concurrency behavior more predictable.

For example, instead of using flags like:

```cpp
struct RunContext {
  // Other context data...
  bool is_bulk; // Flag to indicate if synchronization is needed
};

// Usage in various places
if (!context.is_bulk) {
  // Perform synchronization
}
```

Prefer a centralized approach where synchronization is managed by a specialized component:

```cpp
struct RunContext {
  // Other context data...
  void *event_pool = nullptr; // Pointer to centralized event management
};

// Centralized synchronization management
void SynchronizeOperations(RunContext* ctx) {
  // Handle all synchronization based on events from event_pool
}
```

This pattern is particularly important for CUDA operations where proper event ordering and stream synchronization are critical for correctness. Using centralized event pools and dependency tracking helps ensure operations complete in the correct order without unnecessary synchronization points.
