lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]




I went through the code, you clearly gave the queue more thought than I did :). But accessing it is only the smaller part of the problem. The biggest issue for me is the notification (to the Lua state) that some data has been pushed into the queue. I am worried about resources there, preventing busy-wait or short timeout polling, hence waking up a select() with a UDP packet.
Is there a better way to do this?


You can use a mutex-free Fifo queue between threads. You create a pipe between the two threads. The lua thread then simply adds the file descriptor to the event loop (just like a socket would) and comes back to read the data. The data itself is *not* passed in the pipe, it is shared in memory. You just write a single char to notify the file descriptor on the other side of the pipe.

I have an implementation of a C++ template (parametrized on the type of object you need to pass between threads) that holds in a single header [1].

I would definitely not use UDP for this case because of all the setup overhead and packet (and packet size) encoding issues.

Gaspard

[1] https://github.com/lubyk/lubyk/blob/master/modules/lk/include/lubyk/Fifo.h