1

The Scenario:

I have written a CLI Backdoor and a CLI Controller in C for Linux. I am using Glade along with GTK+3 to create a GUI interface for the Controller. I want the Controller (basically a server with reversed functionality) to accept connections all the time from the backdoor (a client with reversed functionality). Meanwhile, I want that the commands written on a TextBuffer widget in the Controller be sent to the Backdoor and the received output be written back onto it.

Question 1: Should I use threads for the purpose? Question 2: If not, then what would be the best solution to this problem? Question 3: Should I be using GDK Threads? Any tutorials that you may recommend?

Thanks a-million!

1 Answers1

1

Question 1: No, you don't necessarily need threads for this; although they could make sense depending on the use case. They might prevent blocking your UI, or over-complicate your code for no good reason. Note also that GTK itself is single-threaded.

Question 2: Basically, you want to make sure that your code runs concurrently, but that doesn't necessarily imply parallellism, unless part of your code is going to be taking a while, thus blocking the main thread. GLib, the base library for GTK (and a lot of other projects), provides an event loop that can be used to call and implement asynchronous operations, whether they are running in a different thread or not. As long as they notify the main thread, which can be done adding an event the main loop.

Question 3: Depending on what you exactly need for your use case, there are several options:

  • A general abstraction of input/output streams is GIOStream, with GInputStream and GOutputStream helping out with input/output respectively. For asynchoronous methods, look for the _async suffix, like g_input_stream_read_async().
  • A bit lower level, but still relatively easy to use, is GIOChannel, which provides wrappers around common idioms, like a plain socket fd.
  • If you have a very custom setup, you can use something more lowlevel like a GSource, which will allow you to integrate events into the GLib event loop
nielsdg
  • 2,318
  • 1
  • 13
  • 22