Archive for January, 2006

Q_GLOBAL_STATIC

Creating C++ classes in global space is generally a bad idea, because the order of construction is undefined. On top of that, constructors require execution, thus slowing down your program startup time. Enter Q_GLOBAL_STATIC. It works like this:

Q_GLOBAL_STATIC(Foo, get_foo)

Write this in your global space, and it will define a function called get_foo() that essentially does “new Foo” (if it has not done so already) and returns it. Now, instead of using the object directly, you use the function. This puts you in control of when your global objects are created, and gives you lazy-loading to boot. An atomic integer operation is used for thread-safe construction. In addition, a static template class “wrapper” is defined for your type, so that your object will be properly destructed on exit, just like a normal global type. Granted, this is a slight bit of overhead, but probably less than any object you’d use it with. There’s even Q_GLOBAL_STATIC_WITH_ARGS().

You probably shouldn’t be using globals anyway, but sometimes you need them. Q_GLOBAL_STATIC seems particularly useful with QMutex, the bare-minimum to get you off the ground for creating thread-safe singletons.

Comments (2)

New Delta website

The new Delta site is here, powered by WordPress. Now things look a little nicer, and the site should be easier for me to maintain. Most importantly, the main page and roadmap have been updated to 2006. Also, all of my articles are now blog entries (dated in the past, as appropriate). And there’s a SAPO banner!

Although nearly all of the content discussed on this website is my doing, the Delta website is not meant to be personal. Posts will always be relevant to Delta, Psi, Jabber, and related. You won’t have to worry about me writing about the weather or some cat. What you will see are cool Qt/C++ tricks, software ideas, and progress updates.

Comments

Network Interface Detection

Right now I’m working on detecting when network interfaces come and go, so that, for example, Psi can go online when your network connection becomes active. There is a nice debate as to what constitutes a network connection, but I think for our purposes I’ve decided this will be a non-loopback interface bound to a gateway. This code will be part of IrisNet, which is the new network subsystem for Iris.

Time for platform-specific coding fun again.

Comments (5)

moveToThread ( zero )

You can’t delete a QObject directly unless you do it from the thread it lives in. However, you can use object.moveToThread(0) to disassociate the object from any thread, allowing it to be deleted from anywhere. The drawback is that you can’t use queued signals/slots, but for the kind of object you would do this with it is probably not an issue.

QCA uses this trick to disassociate backend provider QObjects from threads so that they can be cleaned up from any thread. This is mainly for the public key front-end objects which are not QObjects, and so the user has an expectation that they can be copied between threads.

Comments

Bad Behavior has blocked 557 access attempts in the last 7 days.