-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* [ADD] Thread safe Set * [TEST] Thread safe contain and remove task --------- Co-authored-by: N'rick <[email protected]>
- Loading branch information
Showing
3 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// | ||
// File.swift | ||
// | ||
// | ||
// Created by Jeanne Creantor, N'rick (Ordinateur) on 2024-06-04. | ||
// | ||
|
||
import Foundation | ||
|
||
public class ThreadSafeSet<T: Hashable> { | ||
private var set: Set<T> = [] | ||
private let queue = DispatchQueue(label: "com.nuglif.safeThreadSet.\(T.self)", attributes: .concurrent) | ||
|
||
public init(set: Set<T> = []) { | ||
self.set = set | ||
} | ||
|
||
public func insert(_ newElement: T) { | ||
queue.async(flags: .barrier) { | ||
self.set.insert(newElement) | ||
} | ||
} | ||
|
||
public func remove(_ member: T) { | ||
queue.async(flags: .barrier) { | ||
self.set.remove(member) | ||
} | ||
} | ||
|
||
public func removeAll() { | ||
queue.async(flags: .barrier) { | ||
self.set.removeAll() | ||
} | ||
} | ||
|
||
public func contains(_ member: T) -> Bool { | ||
queue.sync { set.contains(member) } | ||
} | ||
|
||
public func first(where predicate: (T) -> Bool) -> T? { | ||
queue.sync { set.first(where: predicate) } | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// | ||
// ThreadSafeSetTests.swift | ||
// | ||
// | ||
// Created by Jeanne Creantor, N'rick (Ordinateur) on 2024-06-04. | ||
// | ||
|
||
import XCTest | ||
|
||
import NGTools | ||
|
||
final class ThreadSafeSetTests: XCTestCase { | ||
|
||
func testConcurrencyInsertionAndRemove() { | ||
let set = ThreadSafeSet<UUID>() | ||
let expectation = XCTestExpectation(description: "Testing") | ||
|
||
let queueA = DispatchQueue(label: "TestQueueA", qos: .background) | ||
let queueB = DispatchQueue(label: "TestQueueB", qos: .background) | ||
let iterations = 1000 | ||
let requests = Array(repeating: UUID(), count: iterations) | ||
var completed = 0 | ||
|
||
requests.forEach { request in | ||
queueA.async { set.insert(request) } | ||
|
||
queueB.async { | ||
set.remove(request) | ||
completed += 1 | ||
if completed == iterations - 1 { | ||
expectation.fulfill() | ||
} | ||
} | ||
} | ||
|
||
wait(for: [expectation], timeout: 3) | ||
} | ||
|
||
func testConcurrencyContainsAndRemove() { | ||
let expectation = XCTestExpectation(description: "Testing") | ||
|
||
let queueA = DispatchQueue(label: "TestQueueA", qos: .background) | ||
let queueB = DispatchQueue(label: "TestQueueB", qos: .background) | ||
let iterations = 1000 | ||
let requests = Array(repeating: UUID(), count: iterations) | ||
let set = ThreadSafeSet<UUID>(set: Set(requests)) | ||
var completed = 0 | ||
|
||
requests.forEach { request in | ||
queueA.async { | ||
_ = set.contains(request) | ||
} | ||
|
||
queueB.async { | ||
set.remove(request) | ||
completed += 1 | ||
if completed == iterations - 1 { | ||
expectation.fulfill() | ||
} | ||
} | ||
} | ||
|
||
wait(for: [expectation], timeout: 3) | ||
} | ||
} |