Execute a sequence of instructions without switching to another thread - c++

I have written a MultiThread C++ Codes using boost.
I have the below code in my main thread:
while (!mInputQueue.empty() && mStartProcessJobs)
mProcessJobs.wait(lock);
the second line should be executed immediately after the first line and context switching should not occur. How can I do this?

Depending on the nature of the jobs, you can use an asynchronous service provider.
Often these exist for asynchronous IO (e.g. sockets in non-blocking mode, IO completion ports on windows, libaio etc.)
Boost Asio harnesses all these interfaces (and some more, related to timers, platform specific handles or e.g. serial ports) into a service object. This enables you to run many jobs asynchronously, potentially all on a single thread. This means that there is no context switching.
Asio's io_service has several ways of posting/dispatching jobs. Depending on which you use, jobs might even execute immediately and synchronously.
I suggest you look at some of the samples, as it looks to be precisely what you need.
PS. There are other - more low-level - libraries outside of boost that have the same kind of features but I haven't used them. I think the most popular are libuv/libevent (IIRC)

Related

getting epoll descriptor from boost asio io_service object

Can we get the epoll descriptor underlying the boost asio io_service object ?? we have multiple boost asio io_service objects in our application, one from a library and the other native to the application, the io_service object from the library is a server object serving multiple connections, we are investigating how best we can coordinate multiple io_service objects efficiently.
I'd say you don't need to break the documented interface to combine both.
In fact the documented interface is enough to combine several io_service objects efficiently.
Basically the only point of efficiency you could be looking for is to avoid running separate event loops for them (as that might require more threads than you're prepared to).
As the docs say:
The library interface is decoupled from interfaces for thread creation and management, and permits implementations on platforms where threads are not available.
And the platform specific implementation notes promise (in slightly different but essentially similar wording):
Demultiplexing using epoll is performed in one of the threads that calls io_context::run(), io_context::run_one(), io_context::poll() or io_context::poll_one().
This is your clue. You can knit together many io_services into a single event loop using poll_one() (or even run_one()). In fact, this mechanism can be used to integrate with whatever thrid-party event subsystem you want (libev, Qt idle work etc.). You could call poll_one() in response to a hardware interrupt on systems that don't support threads in the first place.
As a side-note, the inverse is to let other libraries do native socket operations with ASIO doing the polling: Reactor Style Operations.
Both these approaches can be combined.
Summary
Boost Asio is designed to be extensible and to be unintrusive to your design choices. You're most likely able to "fix" your third-party library integration worries using the public interface.
In the recent versions of boost there are methods run_for and run_until, we can wait on the first io service object for a specified time and can call poll on the second io service object when the first one returns or times out.

Signal style IPC for multithreaded linux applications

I have legacy application code written for a real-time operating system (RTOS). Most of the code uses an OS-Specific Inter-Process-Communication (IPC) call, that looks very similar to a signal.
It has two IPC calls:
status_code signal_push(connector, event)
event signal_wait(connector)
I want to slowly port that application to Linux in order to improve testing and debugging.
I want to create a task-description file, that covers tasks their associated IPC primitives with events and handlers.
An example would look like this:
SignalHandler(connector=crypto_connector,
events = [(cyrpto_init, crypto_init_handler),
(crypto_run, crypto_run_handler),
(crypto_done, crypto_done_handler])
Task(name=crypto, priority=1, stack_size=256, connector=crypto_connector)
From this description file it should generate a thread for each task that calls the corresponding handler, that is written by an engineer. it shall also generate call stubs similar to emit_crypto_init()
It is quite clear to me what kind of code I shall generate from this description for the RTOS but I'm still uncertain, what kind of IPC I should use for Linux. For the first version probably anything that keeps behaviour is okay. For future versions, it could be plausible to use Linux on target and therefore the IPC should have little overhead.
I found the following IPC mechanisms:
Unix signals (seems as if it is not an option)
Unix sockets (do a lot more than just sending a signal)
semaphores (do less, but we could use global integers to pass the event to the other party)
Are there more suitable IPC solutions out there, that could fit this requirement?
There are several possibilities:
Boost Signals (not to be confused with Boost Interprocess) - http://www.boost.org/doc/libs/1_51_0/doc/html/signals.html, other is
cpp-events http://code.google.com/p/cpp-events/
libsigc++
Qt Signals and Slots

Getting to know the basics of Asynchronous programming on *nix

For some time now I have been googling a lot to get to know about the various ways to acheive asynchronous programming/behavior on nix machines and ( as known earlier to me ) got confirmed on the fact that there is still no TRULY async pattern (concurrency using single thread) for Linux as available for Windows(IOCP).
Below are the few alternatives present for linux:
select/poll/epoll :: Cannot be done using single thread as epoll is still blocking call. Also the monitored file descriptors must be opened in non-blocking mode.
libaio:: What I have come to know about is that its implementation sucks and its still notification based instead of being completion based as with windows I/O completion ports.
Boost ASIO :: It uses epoll under linux and thus not a true async pattern as it spawns thread which are completely abstracted from user code to acheive the proactor design pattern
libevent :: Any reason to go for it if I prefer ASIO?
Now Here comes the questions :)
What would be the best design pattern for writing fast scalable network server using epoll (ofcourse, will have to use threads here :( )
I had read somewhere that "only sockets can be opened in non-blocking mode" hence epoll supports only sockets and hence cannot be used for disk I/O.
How true is the above statement and why async programming cannot be done on disk I/O using epoll ?
Boost ASIO uses one big lock around epoll call. I didnt actually understand what can be its implications and how to overcome it using asio itself. Similar question
How can I modify ASIO pattern to work with disk files? Is there any recommended design pattern ?
Hope somebody will able to answer all the questions with nice explanations also. Any link to source where the implementation details of epoll and AIO design patterns are exaplained is also appreciated.
Boost ASIO :: It uses epoll under linux and thus not a true async
pattern as it spawns thread which are completely abstracted from user
code to acheive the proactor design pattern
This is not correct. The Asio library uses epoll() by default on most recent Linux kernel versions. however, threads invoking io_service::run() will invoke callback handlers as needed. There is only one place in the Asio library that a thread is used to emulate an asynchronous interface, it is well described in the documentation:
An additional thread per io_service is used to emulate asynchronous
host resolution. This thread is created on the first call to either
ip::tcp::resolver::async_resolve() or
ip::udp::resolver::async_resolve().
This does not make the library "not a true async pattern" as you claim, in fact its name would disagree with you by definition.
1) What would be the best design pattern for writing fast scalable network server using epoll (of course, will have to use threads here :(
)
I suggest using Boost Asio, it uses the proactor design pattern.
3) Boost ASIO uses one big lock around epoll call. I didnt actually
understand what can be its implications and how to overcome it using
asio itself
The epoll reactor uses a mutex to dispatch handlers, though in practice this is not a big concern for most applications. There are application specific ways to mitigate this behavior, such as an io_service per CPU to exploit data locality. See my answer to a similar question on this topic. It is also discussed on the Asio mailing list frequently.
4) How can I modify ASIO pattern to work with disk files? Is there any
recommended design pattern?
The Asio library does not natively support file I/O as you noted. There have been several attempts to add it to the library, I'd suggest discussing on the mailing list.
First of all:
got confirmed on the fact that there is still no TRULY async pattern (concurrency using single thread) for Linux as available for Windows(IOCP).
You probably has a small misconception, asynchronous can be build on top of "polling" api.
More then that "reactor" (epoll-like) API is more powerful then "proactor" API (IOCP) as
the second can be implemented in terms of the first one (but not the other way around).
Also some operations that are "truly" asynchronous for example like disk I/O, some some other tools can be with combination of signals and Linux specific signalfd can provide full coverage of some other cases.
Bottom line. epoll is truly asynchronous I/O

Pollable signalling between threads

I'm working on a project, where a primary server thread needs to dispatch events to a series of worker threads. The work that goes on in the worker threads relies on polling (ie. epoll or kqueue depending on the UNIX system in question) with timeouts on these operations needing to be handles. This means, that a normal conditional variable or semaphore structure is not viable for this dispatch, as it would make one or the other block resulting in an unwanted latency between either handling the events coming from polling or the events originating from the server thread.
So, I'm wondering what the most optimal construct for dispatching such events between threads in a pollable fashion is? Essentially, all that needs to be delivered is a pollable "signal" that tells the worker thread, that it has more events to fetch. I've looked at using UNIX pipes (unnamed ones, as it's internal to the process) which seems like a decent solution given that a single byte can be written to the pipe and read back out when the queue is cleared -- but, I'm wondering if this is the best approach available? Or the fastest?
Alternatively, there is the possibility to use signalfd(2) on Linux, but as this is not available on BSD systems, I'd rather like to avoid this construct. I'm also wondering how great the overhead in using system signals actually is?
Jan Hudec's answer is correct, although I wouldn't recommend using signals for a few reasons:
Older versions of glibc emulated pselect and ppoll in a non-atomic fashion, making them basically worthless. Even when you used the mask correctly, signals could get "lost" between the pthread_sigprocmask and select calls, meaning they don't cause EINTR.
I'm not sure signalfd is any more efficient than the pipe. (Haven't tested it, but I don't have any particular reason to believe it is.)
signals are generally a pain to get right. I've spent a lot of effort on them (see my sigsafe library) and I'd recommend avoiding them if you can.
Since you're trying to have asynchronous handling portable to several systems, I'd recommend looking at libevent. It will abstract epoll or kqueue for you, and it will even wake up workers on your behalf when you add a new event. See event.c
2058 static inline int
2059 event_add_internal(struct event *ev, const struct timeval *tv,
2060 int tv_is_absolute)
2061 {
...
2189 /* if we are not in the right thread, we need to wake up the loop */
2190 if (res != -1 && notify && EVBASE_NEED_NOTIFY(base))
2191 evthread_notify_base(base);
...
2196 }
Also,
The worker thread deals with both socket I/O and asynchronous disk I/O, which means that it is optimally always waiting for the event queuing mechanism (epoll/kqueue).
You're likely to be disappointed here. These event queueing mechanisms don't really support asynchronous disk I/O. See this recent thread for more details.
As far as performance goes, the cost of system call is comparably huge to other operations, so it's the number of system calls that matters. There are two options:
Use the pipes as you wrote. If you have any useful payload for the message, you get one system call to send, one system call to wait and one system call to receive. Try to pass any relevant data down the pipe instead of reading them from a shared structure to avoid additional overhead from locking.
The select and poll have variants, that also waits for signals (pselect, ppoll). Linux epoll can do the same using signalfd, so it remains a question whether kqueue can wait for signals, which I don't know. If it can, than you could use them (you are using different mechanism on Linux and *BSD anyway). It would save you the syscall for reading if you don't have good use for the passed data.
I would expect passing the data over socket to be more efficient if it allows you do do away with any other locking.

Most suitable asynchronous socket model for an instant messenger client?

I'm working on an instant messenger client in C++ (Win32) and I'm experimenting with different asynchronous socket models. So far I've been using WSAAsyncSelect for receiving notifications via my main window. However, I've been experiencing some unexpected results with Winsock spawning additionally 5-6 threads (in addition to the initial thread created when calling WSAAsyncSelect) for one single socket.
I have plans to revamp the client to support additional protocols via DLL:s, and I'm afraid that my current solution won't be suitable based on my experiences with WSAAsyncSelect in addition to me being negative towards mixing network with UI code (in the message loop).
I'm looking for advice on what a suitable asynchronous socket model could be for a multi-protocol IM client which needs to be able to handle roughly 10-20+ connections (depending on amount of protocols and protocol design etc.), while not using an excessive amount of threads -- I am very interested in performance and keeping the resource usage down.
I've been looking on IO Completion Ports, but from what I've gathered, it seems overkill. I'd very much appreciate some input on what a suitable socket solution could be!
Thanks in advance! :-)
There are four basic ways to handle multiple concurrent sockets.
Multiplexing, that is using select() to poll the sockets.
AsyncSelect which is basically what you're doing with WSAAsyncSelect.
Worker Threads, creating a single thread for each connection.
IO Completion Ports, or IOCP. dp mentions them above, but basically they are an OS specific way to handle asynchronous I/O, which has very good performance, but it is a little more confusing.
Which you choose often depends on where you plan to go. If you plan to port the application to other platforms, you may want to choose #1 or #3, since select is not terribly different from other models used on other OS's, and most other OS's also have the concept of threads (though they may operate differently). IOCP is typically windows specific (although Linux now has some async I/O functions as well).
If your app is Windows only, then you basically want to choose the best model for what you're doing. This would likely be either #3 or #4. #4 is the most efficient, as it calls back into your application (similar, but with better peformance and fewer issues to WSAsyncSelect).
The big thing you have to deal with when using threads (either IOCP or WorkerThreads) is marshaling the data back to a thread that can update the UI, since you can't call UI functions on worker threads. Ultimately, this will involve some messaging back and forth in most cases.
If you were developing this in Managed code, i'd tell you to look at Jeffrey Richter's AysncEnumerator, but you've chose C++ which has it's pros and cons. Lots of people have written various network libraries for C++, maybe you should spend some time researching some of them.
consider to use the ASIO library you can find in boost (www.boost.org).
Just use synchronous models. Modern operating systems handle multiple threads quite well. Async IO is really needed in rare situations, mostly on servers.
In some ways IO Completion Ports (IOCP) are overkill but to be honest I find the model for asynchronous sockets easier to use than the alternatives (select, non-blocking sockets, Overlapped IO, etc.).
The IOCP API could be clearer but once you get past it it's actually easier to use I think. Back when, the biggest obstacle was platform support (it needed an NT based OS -- i.e., Windows 9x did not support IOCP). With that restriction long gone, I'd consider it.
If you do decide to use IOCP (which, IMHO, is the best option if you're writing for Windows) then I've got some free code available which takes away a lot of the work that you need to do.
Latest version of the code and links to the original articles are available from here.
And my views on how my framework compares to Boost::ASIO can be found here: http://www.lenholgate.com/blog/2008/09/how-does-the-socket-server-framework-compare-to-boostasio.html.