# Is MouseCallback running in another thread?

**URL:** <https://forum.opencv.org/t/is-mousecallback-running-in-another-thread/18814>\
**Category:** C++\
**Tags:** highgui\
**Created:** [September 24, 2024, 9:33am UTC](https://forum.opencv.org/t/is-mousecallback-running-in-another-thread/18814 "2024-09-24T09:33:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![zzzhhh](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/zzzhhh/32/11163_2.png) [@zzzhhh](https://forum.opencv.org/u/zzzhhh)\
**Post date:** [September 24, 2024, 9:33am UTC](https://forum.opencv.org/t/is-mousecallback-running-in-another-thread/18814/1 "2024-09-24T09:33:34Z")

</div>

Assume I have a main thread which calls namedWindow, setMouseCallback, imread, imshow, etc. Is MouseCallback running in another different thread from the main thread when a mouse event occurs?

This is kind of important to me because both the main thread and MouseCallback write/read the same variable possibly at the same time. If MouseCallback is running in another thread from the main one, I need to write mutex to avoid racing. I did not find related OpenCV document so I ask here. Thanks for you help.

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [September 24, 2024, 12:38pm UTC](https://forum.opencv.org/t/is-mousecallback-running-in-another-thread/18814/2 "2024-09-24T12:38:25Z")

</div>

no, it runs in the context of waitKey()

---

<div class="post-metadata">

**Author:** ![zzzhhh](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/zzzhhh/32/11163_2.png) [@zzzhhh](https://forum.opencv.org/u/zzzhhh)\
**Post date:** [September 24, 2024, 2:20pm UTC](https://forum.opencv.org/t/is-mousecallback-running-in-another-thread/18814/3 "2024-09-24T14:20:13Z")

</div>

> [@crackwitz](#):
>
> context of waitKey

Can you please be more specific about what “context of waitKey” is?

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [September 24, 2024, 4:42pm UTC](https://forum.opencv.org/t/is-mousecallback-running-in-another-thread/18814/4 "2024-09-24T16:42:09Z")

</div>

your callbacks are called in waitKey()

waitKey()'s purpose isn’t to wait for a key. its purpose is to process GUI events.
