coroutines-shared-state

Coordinate Kotlin coroutine access to shared mutable state with Mutex, Atomic primitives, confinement, StateFlow, or Channel-based actors to avoid data races and deadlocks.

Updated Apr 8, 2026
One-click install
npx skills add https://github.com/ClankerGuru/opsx --skill coroutines-shared-state
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: coroutines-shared-state
Source: https://github.com/ClankerGuru/opsx/tree/main/cli/src/main/resources/content/skills/coroutines-shared-state
Command: npx skills add https://github.com/ClankerGuru/opsx --skill coroutines-shared-state

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Coroutines can access and modify shared mutable state, leading to data races and subtle bugs. This skill provides patterns to coordinate access safely and avoid deadlocks.

Core Features & Use Cases

  • Mutex-based synchronization with withLock for short critical sections.
  • Atomic references and StateFlow-based state management for observable, lock-free reads.
  • Single-thread confinement and actor-style messaging to prevent cross-thread contention.
  • Deadlock-avoidance guidance and best practices for lock ordering.

Quick Start

Create a small Kotlin example where multiple coroutines update a shared counter safely and verify thread-safe behavior.

Frequently Asked Questions about coroutines-shared-state

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I prevent data races when multiple Kotlin coroutines access shared mutable state?

You can coordinate access to shared mutable state using Mutex withLock for critical sections, Atomic primitives for lock-free reads, or StateFlow.update to ensure deterministic updates and observable state across coroutines.

What's the best way to synchronize shared state in Kotlin coroutines without causing deadlocks?

Apply Mutex withLock for short critical sections, use Channel-based actors for message passing, and follow lock-ordering best practices to coordinate access while actively avoiding cross-thread contention and deadlocks.

When should I use StateFlow.update versus Atomic primitives for Kotlin coroutine state management?

Use StateFlow.update when you need observable, lock-free state management across multiple coroutines, and use Atomic primitives for simpler shared references where reactive state observation is not required.

Does single-thread confinement work for preventing cross-thread contention in Kotlin coroutines?

Yes, single-thread confinement prevents cross-thread contention by restricting coroutine execution to one thread, eliminating concurrent access to shared mutable state without requiring explicit locks.

How do I implement an actor-style pattern using Channels for shared state in Kotlin coroutines?

Implement actor-style patterns by using Channel-based messaging to serialize state mutations, ensuring multiple coroutines send update messages to a single receiver that processes changes deterministically.

Why does my Kotlin coroutine crash with concurrent modification exceptions on shared data structures?

Concurrent modification exceptions occur because multiple coroutines read and write a common data structure simultaneously; resolve this by applying Mutex withLock, Atomic primitives, or StateFlow.update for deterministic updates.