Signal-Slot Mechanism Explained

C. Burak Ongay
brakulla
Published in
3 min readMay 15, 2019

Signal-Slot Mechanism

In good old function calls, you call a function to make it do a job, and get the result when it returns. Apart from being a perfect way to do something in software, this is how you get something done in almost every programming language. There are two cases in which this is not the answer.

First one, is asynchronous operations. In async operations, you tell some part of the software to do a job, but don’t wait for it to return its result, e.g, it is with database or file operation. You get on doing other things until that returns and when it returns, you collect the result and continue. This is done with callbacks mostly. The callback is a function you pass to another function, for that another function to call when it finishes its job.

A javascript example to callbacks (from this stackoverflow post https://stackoverflow.com/a/7549753):

The result is:

The number is 5
Printing finished

Second one is when an event is occured. Consider an http server, it listens incoming request and knows when a request has come, not you. You tell what it should do when a request comes. In Node.js and express you give a callback to define its action when a request comes.

A javascript with Node.js and express example (from express hello-world example):

When a get request is received by express with request path `/`, it executes your callback function and sends back “Hello World!” string as data of result.

There is a pattern called `observer pattern`, you pass a function (callback, lambda, or whatever similar) to a part of the software for it to call when something happens, without your initiation. You actually register to an event. Multiple listeners can register itself to an event to be notified, and when this event happens, the event handler notifies everyone registered.

In signal-slot mechanism, very similar to observer pattern, you connect a signal to a slot, and when that signal is emitted, all slots are executed. The difference with observer pattern is that neither side needs to know the other. You have a separated concerns, decoupling between two parts of the code, or whatever other explanation that suits this situation.

For the above given example, you have an http server, listening requests. When a request is received, this http server emits its “new request received” signal with the request it received. If you connected this signal to a controller’s slot in main part of the code (not in server or controller code), this slot is executed when that signal is emitted without server knowing the controller or controller knowing the server. They just know what they only should know.

In addition, if these two parts of code are running in separated threads, in observer pattern, the notifier runs the notified code in notifier thread. You can read more about this threading in the Internet. In signal-slot mechanism, the slot runs in slot’s thread even though the signal is emitted in another thread. So, signal-slot mechanism also provides inter-thread communication without a hassle.

Implementation is not part of this post, so if you want to know how to implement such a thing, start reading this!

--

--