# The problem about main thread and worker thread processing the same event

**URL:** <https://www.ruby-forum.com/t/the-problem-about-main-thread-and-worker-thread-processing-the-same-event/217765>\
**Category:** NGINX\
**Created:** [March 12, 2012, 6:23am UTC](https://www.ruby-forum.com/t/the-problem-about-main-thread-and-worker-thread-processing-the-same-event/217765 "2012-03-12T06:23:59Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [March 12, 2012, 6:23am UTC](https://www.ruby-forum.com/t/the-problem-about-main-thread-and-worker-thread-processing-the-same-event/217765/1 "2012-03-12T06:23:59Z")

</div>

In the case of work process is configured to support multi-threading in  
the following scene, there is an event at the same time will be handled  
by the main thread and worker thread.

step1: in ngx\_process\_events\_and\_timers function, the main thread get  
the accept lock and set the ngx\_accept\_mutex\_held to 1, at this time,  
the flags | = NGX\_POST\_EVENTS. And then enter ngx\_process\_events (epoll,  
under for ngx\_epoll\_process\_events function) function, all received  
non-accept epoll events will be added to the the queue  
ngx\_posted\_events;

step2: in the main thread of the work process handler function  
ngx\_worker\_process\_cycle to, inform all the worker threads to process  
events  
in the queue ngx\_posted\_events .

# step3: the main thread to re-enter the event loop, try to grab the accept lock, no grab, ngx\_accept\_mutex\_held be set to 0, then, the flags

1. Then enter ngx\_process\_events, received all non-accept epoll events.  
The flag is not NGX\_POST\_EVENTS, Therefore, directly calling the event  
handler to handle the event.

The problem lies here:  
If in step2 a worker thread is processing a connection event ev1  
(c-\> read or c-\> write), while in step3 connection produced a similar  
event ev1 (c-\> the read or c-\> write), the main thread and worker thread  
handle the same event conflictly.

Posted at Nginx Forum:

> **[The problem about main thread and worker thread processing the same event](https://forum.nginx.org/read.php?2,223741,223741#msg-223741)**
>
> In the case of work process is configured to support multi-threading in the following scene, there is an event at the same time will be handled by the main thread and worker thread. step1: in ngx\_process\_events\_and\_timers function, the main thread...

---

<div class="post-metadata">

**Author:** ![Eagleye\_Tiger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ec9cab/32.png) [@Eagleye\_Tiger](https://www.ruby-forum.com/u/Eagleye_Tiger)\
**Post date:** [March 12, 2012, 11:27am UTC](https://www.ruby-forum.com/t/the-problem-about-main-thread-and-worker-thread-processing-the-same-event/217765/2 "2012-03-12T11:27:05Z")

</div>

Hello!

On Mon, Mar 12, 2012 at 01:23:30AM -0400, Eagleye\_Tiger wrote:

> In the case of work process is configured to support multi-threading in  
> the following scene, there is an event at the same time will be handled  
> by the main thread and worker thread.

Thread support is known to be broken and disabled for reason.

Maxim D.
