I was wonder if it was possible to use a library like this in our crystal program to help schedule events or can we create a crystal shard that can interface with libevent.
bindgen is a binding and wrapper generator for C/C++ libraries for Crystal
Libevent is already used by the crystal standard library to provided evented IO and the sleep function. You shouldn't need to bind libevent manually, just use normal IO, sleep and fibers. You can learn about how crystal handles concurrency here.
Related
Does C++ has some built in timer class with same idea as .NET System.Threading.Timer
http://msdn.microsoft.com/en-us/library/system.threading.timer.aspx
Or I need to implement it myself ? :-(. Maybe there is some library , plz.
Thanks.
Might not be the same but boost has a deadline_timer in asio lib which is quite useful.
http://www.boost.org/doc/libs/1_49_0/doc/html/boost_asio/reference/deadline_timer.html
Standard C++ does not have one built in.
If you are using the Win32 API, which seems likely if you are coming from .NET, there are various options, such as SetTimer (which requires a message loop), or CreateTimerQueueTimer (which does not require a message loop).
As said in the comments, most GUI frameworks will have an asynchronous timer available.
If you are looking for something more generic, or something that doesn't require a GUI framework (like the Win32 API), you should have a look at boost::asio.
No, C++ has no Timer class in the language with an interface like System.Threading.Timer.
You compared the language C++ with the .NET runtime library. But you may find a good library that includes a Timer implementation. Boost is a good candidate.
Node.js has a very good and well thought Net API.
I wonder is there a good C++ only implementation of that API as for example LuaNode do for Lua?
Take a look at node.native - attempt to implement api similar to node.js core lib, but with c++11 (and evented IO is also based on libuv)
There is nothing very similar that I know of.
However there are several reactor frameworks out there which give the same event queue driven environment. For example boost::asio provides an event queue that makes callbacks to handle network events, timers, and arbiatary events that you push onto the event queue.
It's largely the same idea, used in the same way. Howver it's nowhere near as simple as node.js to get started with, and does provide any non-blocking functions other than the basics I said above.
It does provide you with the environment to build your own system though. It's an excellent library, but probably rather lower level than you are looking for. There are other simiar libraries such as ACE and parts of the POCO c++ libraries too, but again, they are lower level than node.js with much less library support.
edit:
I've not looked at it too much but how about this https://github.com/joyent/libuv . This is a library that is used to implement some of the node.js features in a cross platform way. Maybe it's possible to use some of it for what you need?
Boost.Asio is conceptually very similar to Node.js. The primary difference being Asio is implemented as a library and Node.js is a language construct. Asio therefore exposes the event queue, requiring some initial setup to post callback handlers, and eventually a blocking call to start the event loop (io_service.run()).
If you're looking for a pure C++ API similar to Node.js, Boost.Asio is definitely the way to go. It is the de-facto networking library for many C++ applications. It's also discussed heavily on SO in the boost-asio tag.
I'm quite sure you could embed a Javascript engine into your program
v8 (building guide)
SpiderMonkey (building guide)
Actually tying that to your C code needs tinkering with the eval functions of both, but I think I remember seeing sample programs doing that for both engines
Please tell me the most painless way of porting WSAAsyncSelect() function to GCC...
While select() and pselect() may work for your application, they are very much not the same thing as WSAAsyncSelect(). Those functions let you do controlled blocking on an otherwise non-blocking socket, or collection of sockets. The same goes for poll().
Winsock's asynchronous sockets, on the other hand, do not block, ever. There's also the large matter of async notifications, which your code doubtless depends on.
I do not believe there are any native APIs on OS X that provide similar behavior. However, it's possible to build up such a thing. A little Googling turned up CocoaAsyncSocket.
If you would rather not depend on third-party libraries, I suggest building up something on top of Cocoa's CFSocket, as the CocoaAsyncSocket developers did, if you will be porting over a GUI program, rather than dig down to core functions like select(). There's something to be said for using a single development framework for everything.
If you need your code to be cross-platform, the wxWidgets library has the wxSockets* class hierarchy, which emulates the Winsock async socket mechanism. Overall, wxWidgets is structured much like MFC, which eases porting if you're familiar with that.
I believe you want to look at the select function
I use pselect function.
I'm looking for a cross-platform library in C/C++ which can schedule jobs, function calls, etc. It would be nice if it is closer to Java Quartz. I would prefer BSD style licenses, LGPL would be okay too.
Libevent: http://www.monkey.org/~provos/libevent/ is probably too heavyweight for your use case, but you can decide for yourself if it works for you.
Edit: This is more about scheduling functions after certain timeouts within a program. Looking at Quartz, it appears to be broader. So I doubt if libevent is what you are looking for.
First, I'm using Qt at the moment. However, I want the program eventually able to run without a GUI environment, leaving the graphical aspects for configuration mainly. The program makes hefty use of Qt timers and signals/slots, partially for QtScript. So if I want to make it non-GUI operable, hopefully parts of Qt can run without a GUI environment. If not, maybe I'll look into a different Javascript implementation, although QtScript is very convenient how it integrates into Qt's and C++'s OO structure. First, can parts of Qt be used in a non-GUI environment, and if not what other choices are there as far as an events and scheduling library? Preferably OO design.
If you don't use the QtGui module, you don't need a GUI. QtCore etc. will work just fine.
Have you looked at the
Boost.Signals library? (I haven't used it myself.)
libevent may be what you are looking for. This is in C, however.
The libevent API provides a mechanism to execute a callback function when a specific event occurs on a file descriptor or after a timeout has been reached. Furthermore, libevent also support callbacks due to signals or regular timeouts.
libsigc++ has a signals and slots mechanism very similar to Qt's though it's pure C++ (no extra pre-processor). It can also be used with gtkmm, a C++ binding for GTK+.
That said, I'd be surprised if Qt requires that you have a GUI, so you'll probably be able to stick with Qt.
The Poco project offers two interesting solutions:
Notification center: based on Cocoa/OpenStep's NSNotificationCenter
Events and delegates
The Boost signals library is very nice too, but it's one of the few boost libraries that need to be built and linked with.