# Why there is a mutex in sim2\_actor

**URL:** https://forums.foundationdb.org/t/why-there-is-a-mutex-in-sim2-actor/2893
**Category:** FoundationDB Core
**Created:** [September 18, 2021, 6:07am UTC](https://forums.foundationdb.org/t/why-there-is-a-mutex-in-sim2-actor/2893 "2021-09-18T06:07:53Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![ming535](https://sea1.discourse-cdn.com/foundationdb/user_avatar/forums.foundationdb.org/ming535/32/1119_2.png) [@ming535](https://forums.foundationdb.org/u/ming535)
#### Post date: [September 18, 2021, 6:07am UTC](https://forums.foundationdb.org/t/why-there-is-a-mutex-in-sim2-actor/2893/1 "2021-09-18T06:07:53Z")

</div>

Hi, I was looking into code of fdb’s simulation related stuff, and I saw that Sim2 actually uses mutex. My understanding is that the whole system is running inside a thread when in simulation model; so why there is a need to use mutex? For example, in sim2\_actor.cpp

```auto
mutex.enter();
tasks.push(Task(time + seconds, taskID, taskCount++, machine, f));
mutex.leave();

```

---

<div class="post-metadata">

### Author: ![jzhou](https://sea1.discourse-cdn.com/foundationdb/user_avatar/forums.foundationdb.org/jzhou/32/445_2.png) [@jzhou](https://forums.foundationdb.org/u/jzhou)
#### Post date: [September 25, 2021, 4:41pm UTC](https://forums.foundationdb.org/t/why-there-is-a-mutex-in-sim2-actor/2893/2 "2021-09-25T16:41:20Z")

</div>

That’s a good question. I tried to run a simulation using `lldb` and added breakpoints in the `runLoop()`. From what I can tell, there is indeed only one thread running.

@Evan, do you know the reason of using mutex here?

---

<div class="post-metadata">

### Author: ![alexmiller](https://sea1.discourse-cdn.com/foundationdb/user_avatar/forums.foundationdb.org/alexmiller/32/326_2.png) [@alexmiller](https://forums.foundationdb.org/u/alexmiller)
#### Post date: [September 27, 2021, 9:00pm UTC](https://forums.foundationdb.org/t/why-there-is-a-mutex-in-sim2-actor/2893/3 "2021-09-27T21:00:51Z")

</div>

There’s things that need to be able to queue up work into flow. When sim2 is used from an fdb client, then I’d think most operations would work this way? However, I still think fdbserver will occasionally activate work from other threads (e.g. when libeio completes a future?)

---

<div class="post-metadata">

### Author: ![ajbeamon](https://sea1.discourse-cdn.com/foundationdb/user_avatar/forums.foundationdb.org/ajbeamon/32/13_2.png) [@ajbeamon](https://forums.foundationdb.org/u/ajbeamon)
#### Post date: [September 30, 2021, 7:22pm UTC](https://forums.foundationdb.org/t/why-there-is-a-mutex-in-sim2-actor/2893/4 "2021-09-30T19:22:35Z")

</div>

Apparently I wrote this a long time ago while adding some sort of support for running threads in simulation. It looks like there are a couple unit tests in the multi-version client that might make use of this, and there’s a workload that uses it as well that I don’t think we actually run in simulation.
