Alok Save
Alok Save

Reputation: 206546

Thread Context Switching

I am working on a C/C++ project which involves a UI and a background service which does some heavy processing by fetching data over the network. In order that my UI doesn't become unresponsive, I would want to spawn a separate thread and then call the background service within that thread, while another thread will display a busy indication in the UI. Once the background service completes it's job, I would want to switch the thread context from background thread to the UI thread, So that the busy indication gets removed and further screen flows can be shown. I believe this is the usual way most of the UI works. What i would want to know is what are the efficient and best ways of achieving this. Right now i do not have a code implemented for the above and I am just asking for ideas and best ways of doing this.

The platform is Linux. The UI framework I am using is a custom UI framework which provides a SDK for UI development but unlike most UI frameworks would it provides nothing for the scenario I mentioned. It is almost deprecated framework but needs to be used for this project which is a pain so only option is I can use Pthread or System v with some wrappers over actual calls which would help me keep the implementation portable on different platforms.

I cannot use Boost Threads because of certain limitations of the embedded environment.
Any suggestions and explanation about how the context switch takes place will be really helpful.

Upvotes: 3

Views: 5553

Answers (3)

lemic
lemic

Reputation: 197

Let me try to help you with a high level answer.

You can indeed look at posix threads (one for UI, one or more for backend processing). You will need to synchronize between them of course; which means also looking at topics such as mutex, volatile keyword or spin lock.

Depending on your degree of comfort with each, try to search for these topics in past questions.

good luck

Upvotes: 0

bobah
bobah

Reputation: 18864

I have a sample code to create and use thread and mutex with pthreads. Threads in example concurrently lock mutex/increment variable/unlock mutex. A missing bit which you may need is a conditional variable (same initialization pattern), then will have full set of minimum required primitives for pthread-based implementation of your task runner thread. And you won't depend on any thirdparty library.

Upvotes: 0

zvrba
zvrba

Reputation: 24546

First, you are confused about how threads work. You can't switch thread contexts at will, the operating system does that whenever it feels like it.

Second, if your concern is context-switch overhead, you are doing something very wrong.

Lastly, UIs usually have event-driven architecture. Usually, you have a "main" thread running an event loop which processes various events. So the proper way to do what you need is to have the background thread post progress events to the "main" thread. And you have to do that in a thread-safe manner; the exact details are dependent on the UI toolkit.

Upvotes: 6

Related Questions