开发者

How to wake a thread after receiving 2 packets

开发者 https://www.devze.com 2023-01-26 08:29 出处:网络
I\'m using libnetfilter_queue for my project. From C app queue is accessible by \"queue file descriptor\". I have 5 queues and 5 threads t开发者_C百科o handle them. What I want to achieve is to wake t

I'm using libnetfilter_queue for my project. From C app queue is accessible by "queue file descriptor". I have 5 queues and 5 threads t开发者_C百科o handle them. What I want to achieve is to wake thread when there is exactly 2 packets in queue. I came up with idea to use select function and array of ints indicating how many packets were queued in each queue. After select exit with > 0 code I check which queue has received a packet and increment value in array, if it's bigger than 2 I wake up a thread. Everything would be fine, but select indicate that queue has data to read until I call recv and I can't do that because separate thread should handle these packets. Anyone has idea how to solve this issue? I know I can set SO_RCVLOWAT but it does not solve my problem, because I don't know what size will be those 2 packets.


As recommended by Tobu, epoll is a better choice and it performs better than select. However, most of these polling functions will indicate there is an event (data available) unless someone reads. If possible use the following model: Use epoll/select to watch for the incoming data wake up the worker thread. Let the worker thread decide what to do with the data (one packet, two or more) before actually doing the work.

OR: One Reader thread-N Worker threads: Will use epoll to wait and read all the incoming data and post it to the corresponding worker thread's queue. Once the # of packet reaches the threshold, wake up the Worker thread (using a semaphore).


You are looking for edge-triggered event notifications — notifications that are sent when the quantity of available data changes. epoll works like that when using the EPOLLET flag, and by default will rearm the notification so that you keep being notified of new packets.

Please note that you will be notified only once if several packets arrive between two epoll_wait calls.

0

精彩评论

暂无评论...
验证码 换一张
取 消