Windows APCs and Alertable Waits: Thread-Affine Completion Callbacks
Understand when user-mode APCs run, how alertable waits deliver completions, and why APC callbacks require strict thread-affine lifetime and reentrancy rules.
tag
26 posts
Understand when user-mode APCs run, how alertable waits deliver completions, and why APC callbacks require strict thread-affine lifetime and reentrancy rules.
Use CancelIoEx without freeing buffers too early: target one OVERLAPPED request, await its terminal result, and handle completion races explicitly.
Implement delayed clipboard rendering with correct owner lifetime, message-specific OpenClipboard rules, movable memory, and bounded UI-thread work.
Trace Windows sharing violations by separating access requests, reciprocal share permissions, delete/rename semantics, byte-range locks, and ACL authorization.
Call CreateProcessW with an explicit executable path and writable command line, then account for CRT quoting rules, parser differences, and handle inheritance.
Build a resilient Windows directory watcher with ReadDirectoryChangesW, overlapped I/O, bounded buffers, overflow recovery, and reconciliation against current state.
Build a durable Windows Event Log consumer with bounded XPath queries, push or pull delivery, persisted bookmarks, stale-result handling, and idempotent processing.
Use Windows file mappings safely: distinguish mapping objects from views, align offsets, synchronize shared memory, and separate page flushing from durable storage.
Measure per-process GDI and USER object trends, identify leaking creation paths, and fix ownership errors without raising quotas as a first response.
Enable long-path-aware behavior deliberately, identify legacy API boundaries, and test absolute, UNC, shell, and library paths without breaking deployment tools.
Design Windows named-pipe servers with explicit ACLs, reusable instances, overlapped connections, message framing, and safe client impersonation boundaries.
Use INIT_ONCE to initialize shared Windows state exactly once, handle retries and context ownership, and avoid loader-lock, reentrancy, and teardown races.
Handle mixed-DPI monitor changes in Win32 with Per-Monitor v2 awareness, WM_DPICHANGED, suggested bounds, and DPI-specific layout and assets.
Use Windows power requests with explicit reason strings and balanced clear operations, while respecting Modern Standby, user sleep, battery, and shutdown policy.
Use RegNotifyChangeKeyValue as a one-shot invalidation signal, with least-privilege handles, thread-lifetime safeguards, bounded rescans, and race-aware rearming.
Understand Windows RPC binding handles, endpoint mapping, protocol sequences, authentication, and cleanup when diagnosing client-server call failures.
Implement Windows service control handlers that return promptly, report pending checkpoints, honor shutdown budgets, and avoid blocking the Service Control Manager.
Pair SRW locks with Windows condition variables for reader-writer access and blocking queues, while accounting for wake races, non-recursion, and shutdown.
Use Win32 synchronization barriers for phased parallel work, with a fixed participant count, safe leader work, and explicit handling for cancellation and stalls.
Use Windows TLS for per-thread state with explicit slot and value cleanup, and choose FLS or C++ thread_local when the execution model requires it.
Use PostThreadMessage only with a live queue and compatible message loop, and avoid lost notifications, modal-loop surprises, and synchronous-send deadlocks.
Use Windows thread-pool cleanup groups to cancel queued callbacks, wait for running work, and release callback contexts without use-after-free or shutdown deadlocks.
Register a Windows thread-pool wait on one event, re-arm it deliberately, and cancel queued callbacks before closing handles or freeing callback state.
Manage legacy Windows timer-queue callbacks safely: prevent overlap surprises, stop new expirations, wait for callbacks, and release callback state last.
Understand Windows virtual-address reservation, commit charge, page faults, protection changes, and VirtualFree release rules before diagnosing memory growth.
Use waitable timers as synchronization objects, interpret relative due times correctly, and handle periodic signals, cancellation races, and power costs.