# How to properly use custom executor on global actor?

**URL:** <https://forums.swift.org/t/how-to-properly-use-custom-executor-on-global-actor/71829>\
**Category:** Using Swift\
**Tags:** concurrency, globalactor, executor\
**Created:** [May 14, 2024, 12:18pm UTC](https://forums.swift.org/t/how-to-properly-use-custom-executor-on-global-actor/71829 "2024-05-14T12:18:18Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![filiplazov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/filiplazov/32/7886_2.png) [@filiplazov](https://forums.swift.org/u/filiplazov)\
**Post date:** [May 14, 2024, 12:18pm UTC](https://forums.swift.org/t/how-to-properly-use-custom-executor-on-global-actor/71829/1 "2024-05-14T12:18:18Z")

</div>

Hi Everyone

I am experimenting with creation of global actor `MyGlobalActor` with a custom serial executor that is supposedly dispatching / enqueueing tasks on a specific dispatch queue `MyGlobalActorQueue`.  
When I try to apply the global actor to a custom service (class) type, calls to that function don't seem to use the serial executor I created, the way I checked is by putting a breakpoint on the executor `enqueue` function.  
Am I doing something wrong, or is this a bug?

Here is my custom executor

```swift
import Dispatch

final class GlobalActorSerialExecutor: SerialExecutor {
  private let queue = DispatchQueue(label: "MyGlobalActorQueue")

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

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

```

My global actor

```swift
@globalActor actor MyGlobalActor: GlobalActor {
  static let shared = MyGlobalActor()
  static var sharedUnownedExecutor: UnownedSerialExecutor {
    GlobalActorSerialExecutor().asUnownedSerialExecutor()
  }
}

```

My class annotated as my global actor

```swift
@MyGlobalActor
final class MyService {
  var state = 0

  func doStuff() {
    state += 1
  }
}

```

and somewhere at call site

```swift
Task {
  let service = await MyService()
  await service.doStuff()
}

```

Thanks  
Filip

---

<div class="post-metadata">

**Author:** ![vns](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/vns/32/37396_2.png) [@vns](https://forums.swift.org/u/vns)\
**Post date:** [May 14, 2024, 12:42pm UTC](https://forums.swift.org/t/how-to-properly-use-custom-executor-on-global-actor/71829/2 "2024-05-14T12:42:35Z")

</div>

> [@filiplazov](#):
>
> ```swift
> static var sharedUnownedExecutor: UnownedSerialExecutor {
> GlobalActorSerialExecutor().asUnownedSerialExecutor()
> }
> 
> ```

That’s not how you implement custom executor for actor. You need to define

```swift
nonisolated var unownedExecutor: UnownedSerialExecutor

```

on actor instance type, then `shared` instance of an actor will use it.

You also can make static executor property in case you want to share executor between instances or allow other actors use it:

```swift
@globalActor 
actor MyGlobalActor: GlobalActor {
    static let shared = MyGlobalActor()
    static let sharedUnownedExecutor: UnownedSerialExecutor = GlobalActorSerialExecutor()
        .asUnownedSerialExecutor()

    nonisolated var unownedExecutor: UnownedSerialExecutor { 
        Self.sharedUnownedExecutor 
    }
}
```

---

<div class="post-metadata">

**Author:** ![ktoso](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ktoso/32/44526_2.png) [@ktoso](https://forums.swift.org/u/ktoso)\
**Post date:** [May 14, 2024, 12:44pm UTC](https://forums.swift.org/t/how-to-properly-use-custom-executor-on-global-actor/71829/3 "2024-05-14T12:44:54Z")

</div>

Yep that’s the right way.

One extra note is to watch out that the actors executor lifetime extends beyond the actors lifetime — the “unowned executor” quite literarily is not owned (retained) by anything, so make sure the actor or some global thing retains it while things are supposed to be running on it

---

<div class="post-metadata">

**Author:** ![filiplazov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/filiplazov/32/7886_2.png) [@filiplazov](https://forums.swift.org/u/filiplazov)\
**Post date:** [May 14, 2024, 1:02pm UTC](https://forums.swift.org/t/how-to-properly-use-custom-executor-on-global-actor/71829/4 "2024-05-14T13:02:58Z")

</div>

Thanks @vns and @ktoso

I edited @vns's snippet to actually retain the executor, otherwise I got a crash.

```swift
@globalActor
actor MyGlobalActor: GlobalActor {
  static let shared = MyGlobalActor()
  private static let executor = GlobalActorSerialExecutor()
  static let sharedUnownedExecutor: UnownedSerialExecutor = MyGlobalActor.executor
    .asUnownedSerialExecutor()

  nonisolated var unownedExecutor: UnownedSerialExecutor {
    Self.sharedUnownedExecutor
  }
}

```

It now works.  
Thank s a lot for your help, finding info and samples on how to do this is difficult, I tried for 2 hours :)

---

<div class="post-metadata">

**Author:** ![ktoso](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ktoso/32/44526_2.png) [@ktoso](https://forums.swift.org/u/ktoso)\
**Post date:** [May 14, 2024, 1:22pm UTC](https://forums.swift.org/t/how-to-properly-use-custom-executor-on-global-actor/71829/5 "2024-05-14T13:22:11Z")

</div>

Thank you for the feedback, we're aware the documentation is lacking and will try to improve the situation.

Today the best resource for things is going to be the evolution proposals: [swift-evolution/proposals/0392-custom-actor-executors.md at main · apple/swift-evolution · GitHub](https://github.com/apple/swift-evolution/blob/main/proposals/0392-custom-actor-executors.md)

For this specific topic I think we could potentially add a section in here [SerialExecutor | Apple Developer Documentation](https://developer.apple.com/documentation/swift/serialexecutor)... I'll look into it.

---

<div class="post-metadata">

**Author:** ![filiplazov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/filiplazov/32/7886_2.png) [@filiplazov](https://forums.swift.org/u/filiplazov)\
**Post date:** [May 14, 2024, 1:34pm UTC](https://forums.swift.org/t/how-to-properly-use-custom-executor-on-global-actor/71829/6 "2024-05-14T13:34:57Z")

</div>

> [@ktoso](#):
>
> For this specific topic I think we could potentially add a section in here [SerialExecutor | Apple Developer Documentation](https://developer.apple.com/documentation/swift/serialexecutor)... I'll look into it.

Thats a great Idea, I actually opened that documentation and expected to find a sample / programming guide on how to create one before looking on the forums.

When it comes to the swift evolution, I do look for things there quite often, but sometimes, terminology used in the swift evolution titles might not come obvious to users of swift (as opposed to ones developing it). I have no idea on how to improve that really.

BR  
Filip

---

<div class="post-metadata">

**Author:** ![ktoso](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ktoso/32/44526_2.png) [@ktoso](https://forums.swift.org/u/ktoso)\
**Post date:** [May 15, 2024, 3:08am UTC](https://forums.swift.org/t/how-to-properly-use-custom-executor-on-global-actor/71829/7 "2024-05-15T03:08:14Z")

</div>

I took a stab at it over here: [[Concurrency] Document custom executors in API docs a bit by ktoso · Pull Request #73634 · apple/swift · GitHub](https://github.com/apple/swift/pull/73634) -- there's more things that should be included here, like global actors etc, but that's a first stab triggered by this thread.

We'll keep improving documentation as we polish up the Swift 6 release, thank you for reminding me about this.

---

<div class="post-metadata">

**Author:** ![hassila](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hassila/32/22647_2.png) [@hassila](https://forums.swift.org/u/hassila)\
**Post date:** [May 15, 2024, 3:48am UTC](https://forums.swift.org/t/how-to-properly-use-custom-executor-on-global-actor/71829/8 "2024-05-15T03:48:16Z")

</div>

> [@ktoso](#):
>
> we're aware the documentation is lacking and will try to improve the situation.

I have this dream that we’d soon be able to feed all the evolution proposals to an LLM and generate supplementary docs to TSPL…

---

<div class="post-metadata">

**Author:** ![eskimo](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/eskimo/32/13558_2.png) [@eskimo](https://forums.swift.org/u/eskimo)\
**Post date:** [May 15, 2024, 7:45am UTC](https://forums.swift.org/t/how-to-properly-use-custom-executor-on-global-actor/71829/9 "2024-05-15T07:45:08Z")

</div>

… which will then [mislead folks horribly](https://forums.swift.org/t/preferencekey-protocol-conflicts-with-swift-concurrency/71821/3) )-:

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple

---

<div class="post-metadata">

**Author:** ![hassila](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hassila/32/22647_2.png) [@hassila](https://forums.swift.org/u/hassila)\
**Post date:** [May 15, 2024, 8:27am UTC](https://forums.swift.org/t/how-to-properly-use-custom-executor-on-global-actor/71829/10 "2024-05-15T08:27:17Z")

</div>

Sure, thus still a dream ;-)

I guess my main point is that the proposals are a treasure trove of information that is usually not covered elsewhere in docs, it would be amazing if we could find a way to consolidate that information somehow.
