# Using DispatchQueue as actor's serial executor under Linux

**URL:** <https://forums.swift.org/t/using-dispatchqueue-as-actors-serial-executor-under-linux/75260>\
**Category:** Server\
**Tags:** concurrency, libdispatch, actors, dispatchqueue\
**Created:** [October 9, 2024, 7:54pm UTC](https://forums.swift.org/t/using-dispatchqueue-as-actors-serial-executor-under-linux/75260 "2024-10-09T19:54:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mman](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mman/32/1243_2.png) [@mman](https://forums.swift.org/u/mman)\
**Post date:** [October 9, 2024, 7:54pm UTC](https://forums.swift.org/t/using-dispatchqueue-as-actors-serial-executor-under-linux/75260/1 "2024-10-09T19:54:45Z")

</div>

I am trying to make an `actor` backed by serial `DispatchQueue` so that I can interoperate with legacy queue based code, but the following example working under macOS fails under Linux.

Am I holding it wrong?

```swift
public actor MyActor {
    let queue = DispatchSerialQueue(label: "actorQueue")

    nonisolated public var unownedExecutor: UnownedSerialExecutor {
        queue.asUnownedSerialExecutor()
    }
}

```

Under Linux it produces the following error:

```swift
/src/Sources/Test/test.swift:63:17: error: cannot find 'DispatchSerialQueue' in scope
 61 | public actor MyActor {
 62 | let logger: Logger
 63 | let queue = DispatchSerialQueue(label: "relayQueue")
    | `- error: cannot find 'DispatchSerialQueue' in scope
 64 | 
 65 | nonisolated public var unownedExecutor: UnownedSerialExecutor {

```

P.S. filed as a bug here: [class DispatchSerialQueue not available under Linux · Issue #848 · apple/swift-corelibs-libdispatch · GitHub](https://github.com/apple/swift-corelibs-libdispatch/issues/848)

---

<div class="post-metadata">

**Author:** ![Jon\_Shier](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jon_shier/32/921_2.png) [@Jon\_Shier](https://forums.swift.org/u/Jon_Shier)\
**Post date:** [October 9, 2024, 7:56pm UTC](https://forums.swift.org/t/using-dispatchqueue-as-actors-serial-executor-under-linux/75260/2 "2024-10-09T19:56:30Z")

</div>

`import Dispatch`?

---

<div class="post-metadata">

**Author:** ![mman](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mman/32/1243_2.png) [@mman](https://forums.swift.org/u/mman)\
**Post date:** [October 14, 2024, 9:41am UTC](https://forums.swift.org/t/using-dispatchqueue-as-actors-serial-executor-under-linux/75260/3 "2024-10-14T09:41:22Z")

</div>

Nope, the `DispatchSerialQueue` is simply missing under Linux.

The proper solution to make an `actor` that uses `DispatchQueue` as its executor so that it can cooperate with queue based API is for now this:

```swift
actor X {
    let queue = DispatchQueue(label: "queue")
    private let executor: DispatchQueueExecutor

    final class DispatchQueueExecutor: SerialExecutor {
         private let queue: DispatchQueue

         init(queue: DispatchQueue) {
             self.queue = queue
         }

         func enqueue(_ job: UnownedJob) {
             self.queue.async {
                 job.runSynchronously(on: self.asUnownedSerialExecutor())
             }
         }

         func asUnownedSerialExecutor() -> UnownedSerialExecutor {
             UnownedSerialExecutor(ordinary: self)
         }

        func checkIsolated() {
            dispatchPrecondition(condition: .onQueue(self.queue))
        }
    }

    nonisolated public var unownedExecutor: UnownedSerialExecutor {
        executor.asUnownedSerialExecutor()
    }

    init() {
        self.executor = DispatchQueueExecutor(queue: queue)
    }

    /// remaining logic
}

```
