LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-18-2012, 09:52 PM   #1
Skaperen
Senior Member
 
Registered: May 2009
Location: WV, USA
Distribution: Slackware, Ubuntu, Amazon Linux
Posts: 1,850
Blog Entries: 21

Rep: Reputation: 119Reputation: 119
libraries with event loops (C language)


Over the years of programming I've seen quite a number of libraries that have event loops as part of their API. These are generally some kind of communications or interactive device library. You've seen them (or you won't be able to help), and perhaps have used them. I have used a couple in the past.

Now I have a project in mind which needs a few libraries, and TWO of them require using their event loop. This seems to generally impose a conflict. The only way I see to deal with this is make child processes to handle the things with these libraries, and shift the interface design to passing messages (structs) in sockets between these "driver proceses" and the master that is implementing the application. Then the main loop is select-ing or poll-ing these sockets.

I'm wondering if there is any general solution to this conflict without having to go to additional processes. One worry is that some of the libraries I've seen that do these event loops, don't even allow checking a file descriptor during their event loop (so the only wakeup comes from the resource the library is managing).

I'd rather not say what libraries I'm looking at, because I'm asking this in the general context, rather than for specific libraries. I'm wondering if there are general solutions.
 
Old 10-19-2012, 06:57 AM   #2
a4z
Senior Member
 
Registered: Feb 2009
Posts: 1,717

Rep: Reputation: 717Reputation: 717Reputation: 717Reputation: 717Reputation: 717Reputation: 717Reputation: 717
if there is no need to run in parallel, than your main loop can have multiple childs and call loop on each child in each loop.
if they need to run in parallel, than you have some kind of 'standard' "how to synchronize" question.
could be that socket is a good solution, could also be that is is enough and use a mutex for something like an internal message queue and your childloops are running in threads, or a semaphore, that depends on the details of what you want to make.
 
Old 10-19-2012, 08:32 AM   #3
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,397
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
I think it boils down to the quality of the design of the library. Often, there are hooks exposed in the API that allow you to call a function periodically, say from your own main event loop in order for the library to service it's requirements. Or, you launch the library's main routine, and provide one or more callbacks that it uses to let your own code execute. Some of these are often described as 'idle' callbacks; the library code gives up execution whenever it isn't doing something else. If the libraries in question are open-source, it doesn't seem unreasonable to be able to add some hooks like idle callbacks. I imagine the library author(s) would appreciate such a contribution to the project.
I have yet to see any significant consistency to how such library APIs are designed. I'm not sure it is possible, over a wide range of purposes served by such libraries. It does seem like a design pattern that has yet to be well addressed, and is begging for a general purpose framework to allow future designs to co-exist more easily.

The methods you suggest seem like the only obvious solution.

Very interesting question. I hope others with significant experience can chime in.

--- rod.
 
Old 10-19-2012, 11:41 PM   #4
Skaperen
Senior Member
 
Registered: May 2009
Location: WV, USA
Distribution: Slackware, Ubuntu, Amazon Linux
Posts: 1,850
Blog Entries: 21

Original Poster
Rep: Reputation: 119Reputation: 119
Quote:
Originally Posted by theNbomr View Post
I think it boils down to the quality of the design of the library. Often, there are hooks exposed in the API that allow you to call a function periodically, say from your own main event loop in order for the library to service it's requirements.
This would still require some significant design effort to avoid ending up with something that just spins in the CPU usage state checking everything constantly. The more you slow it down with a time wait, the less responsive it becomes.

I read some about the underlying X protocol some time back. The way it was handled, you could have your own main loop as long as you polled its file descriptor (to the X server) and called it when something happened.

Quote:
Originally Posted by theNbomr View Post
Or, you launch the library's main routine, and provide one or more callbacks that it uses to let your own code execute. Some of these are often described as 'idle' callbacks; the library code gives up execution whenever it isn't doing something else. If the libraries in question are open-source, it doesn't seem unreasonable to be able to add some hooks like idle callbacks. I imagine the library author(s) would appreciate such a contribution to the project.
If everything you need to do with this one library can be handled by callbacks, that's fine. But what if you have one library that uses its own main loop for the user interface (GUI or console), and another that operates some network communications.

I have seen some where the calling application can register file descriptors and those fds will be polled with everything else in the main loop. When there is activity for what the library operates, you get library based results (a callback giving a row from a database, or a mouse event location, etc). And when it is a registered fd, you get the fd number.

But you can't make callbacks from one library call the main loop of another library.

Quote:
Originally Posted by theNbomr View Post
I have yet to see any significant consistency to how such library APIs are designed. I'm not sure it is possible, over a wide range of purposes served by such libraries. It does seem like a design pattern that has yet to be well addressed, and is begging for a general purpose framework to allow future designs to co-exist more easily.
The only way I can see is for libraries to NOT use their own main loop (or at least have not using it as an option). They would have to provide some means for a common wakeup from sleep. Those with open fds in non-blocking mode could designate the fd which then a main loop would select/poll. Most of things I think can be reduced to fds. But there might be other things going on in special cases.
 
Old 10-20-2012, 09:30 AM   #5
a4z
Senior Member
 
Registered: Feb 2009
Posts: 1,717

Rep: Reputation: 717Reputation: 717Reputation: 717Reputation: 717Reputation: 717Reputation: 717Reputation: 717
o, I thought you want to do your own library and need 2 main loops:-)

Quote:
Originally Posted by Skaperen View Post
If everything you need to do with this one library can be handled by callbacks, that's fine. But what if you have one library that uses its own main loop for the user interface (GUI or console), and another that operates some network communications.
there is no problem
you have to run one of them or even both of course in parallel, or
I was able to run my own main loop with gtk, wxWdigets(bit tricky) and fltk,
you can query them if they have an event that needs to be to proceed and frame them if required
I did not find out how to do my own mainloop with with QT (so I did not use it where i needed to run may own one)

Quote:
Originally Posted by Skaperen View Post
The only way I can see is for libraries to NOT use their own main loop (or at least have not using it as an option)
some do so, as written above, some do not so.
simply select the right libs for your work, or change their implementation if possible

Last edited by a4z; 10-20-2012 at 09:31 AM.
 
Old 10-22-2012, 09:11 AM   #6
Skaperen
Senior Member
 
Registered: May 2009
Location: WV, USA
Distribution: Slackware, Ubuntu, Amazon Linux
Posts: 1,850
Blog Entries: 21

Original Poster
Rep: Reputation: 119Reputation: 119
Quote:
Originally Posted by a4z View Post
o, I thought you want to do your own library and need 2 main loops:-)
Well you can't do 2 main loops, at least not in a single thread.


Quote:
Originally Posted by a4z View Post
there is no problem
you have to run one of them or even both of course in parallel, or
I was able to run my own main loop with gtk, wxWdigets(bit tricky) and fltk,
you can query them if they have an event that needs to be to proceed and frame them if required
I did not find out how to do my own mainloop with with QT (so I did not use it where i needed to run may own one)
Well, there is a problem. If you call some library that does its own main loop, from within your own main loop, it breaks. That is because your main loop isn't working. Or else you can't run a main loop and you have to depend on the library's main loop to not only handle the library supported events, but all the others as well.

Probably the best solution is a general purpose universal main loop library that does nothing but being a main loop that knows how to handle all events. But how would this be written?


Quote:
Originally Posted by a4z View Post
some do so, as written above, some do not so.
simply select the right libs for your work, or change their implementation if possible
The general problem has its limits. Some libraries are just too poorly coded inside to reasonably modify their main loops. IMHO, libraries should not even have main loops. They should interface to a general purpose main loop.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Python language, its functions and libraries Mr. Alex Programming 4 09-17-2012 10:50 AM
OpenSSL missed libraries. BIGNUM libraries! ChozN Programming 1 12-25-2011 02:38 PM
Thread limits in dynamic libraries as opposed to static libraries pmkenny1234 Linux - Software 0 04-26-2011 02:22 PM
event.d scripting language? Hourani Linux - Newbie 3 02-02-2009 04:39 PM
Fire Event from other event in Visual C++ Express Edition chrisliando Programming 1 11-08-2007 05:12 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:57 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration