1 example

Data race

Concurrent threads improperly accessing shared data.

[ FAQ1 ]

What is a data race?

A data race happens when multiple threads in a program access shared memory concurrently, and at least one thread modifies the memory, without proper synchronization mechanisms in place. Due to the timing unpredictability inherent in multithreading, these unsynchronized operations can yield inconsistent results, corrupt data, or cause runtime errors. Data races undermine thread safety and program reliability. They're common pitfalls in concurrent programming, where threads must interact safely and predictably when working with shared resources.
[ FAQ2 ]

How to fix data race conditions

To fix data race conditions, ensure proper synchronization when accessing shared memory. Utilize synchronization mechanisms like mutexes (locks), semaphores, or synchronized blocks to ensure only one thread can modify shared data at a time. Implement thread-safe data structures and methods specifically designed for concurrent use. In programming languages that support concurrency (such as Java, C++, or Go), clearly define and enforce the rules of thread safety within your codebase. Additionally, use specialized tools and analyzers designed to detect data races early, and consistently apply best practices like minimizing shared state or using immutable data structures.
diff block
+//
+// SystemPrompt.swift
+// Onit
+//
+// Created by Kévin Naudin on 07/02/2025.
+//
+
+import Defaults
+import Foundation
+import SwiftData
+
+@Model
+class SystemPrompt {
+
+ @Attribute(.unique)
+ var id: String
+
+ var name: String
+ var prompt: String
+ var applications: [URL]
+ var tags: [String]
+ var timestamp: Date
+ var lastUsed: Date?
+
+ init() {
+ id = UUID().uuidString
+ name = ""
+ prompt = "Enter instructions to define role, tone and boundaries of the AI"
+ applications = []
+ tags = []
+ timestamp = Date()
+ }
+
+ init(id: String = UUID().uuidString, name: String, prompt: String, applications: [URL], tags: [String], timestamp: Date = Date()) {
+ self.id = id
+ self.name = name
+ self.prompt = prompt
+ self.applications = applications
+ self.tags = tags
+ self.timestamp = timestamp
+ }
+
+ nonisolated(unsafe) static let outputOnly: SystemPrompt = .init(
+ id: "output-only",
+ name: "Output-only response",
+ prompt: "Based on the provided instructions, either provide the output or answer any questions related to it. Provide the response without any additional comments. Provide the output ready to go.",
+ applications: [],
+ tags: []
+ )
Greptile
greptile
logic: nonisolated(unsafe) static property could lead to data races since it's shared across threads. Consider making this an instance method that creates a new SystemPrompt each time, or use proper synchronization.