How to implement a function like invokeOnMainThread() in C/C++ linux? - c++

I'm looking for a way to implement a functional equivalent of invokeOnMainThread(c# xamarin)/ runOnUiThread(android)/ performSelectorOnMainThread:(objective C) in C/C++ on linux.
Essentially what I need is a function, which can be called from any thread, to which I pass a handler, which is executed on the main thread.
I believe this will be helpful in cases where I want to restrict access to a particular resource to a particular thread (eg. libmysql DB access to main thread).
What would be an elegant way to implement this?
How is it done in android?

It is straightforward to have your runOnMainThread function place handlers in a thread-safe queue for later execution by the main thread. Your main thread needs to be prepared to periodically execute handlers it finds in that queue. This implies that the main thread needs to include a loop that periodically checks for new handlers in the queue. This periodic checking can be made efficient through the use of semaphores or other thread-safe signaling mechanisms.
Here are some SO questions on thread safe queues:
C++11 thread-safe queue
is std::queue thread safe with producer and multiple consumers

Related

thread synchronization and priority in CPP

I'm trying to create a task manager, which accepts tasks and runs each task as a new thread, using C++ and (currently) std::thread on a Linux environment .
the task manager accepts normal tasks and priority tasks.
when a priority task arrives, all normal tasks need to be halted until the priority task is done.
I'm keeping all normal task threads in a std::vector, but I couldn't find a proper function to halt those threads.
is there a way, preferably not using locks, to implement the wanted behavior?
maybe with <pthread> or boost threads?
There is no direct way to interrupt a thread from the outside.
Boost interruption points are handy to stop things once for all but that's not equivalent to a pause.
I would suggest you to implement your own "interruption" class with a condition variable (and yes a mutex) to check and wait efficiently anywhere inside your tasks. But it is up to you to explicitely call these interruptions.
Maybe another way would be make your priority tasks multithreadable so that you can allocate more threads to fulfill them => the scheduler is more likely to complete them first but that's not sure so forget what i said.
Sorry, I don't aknowledge anything better then this.

Solutions to replace Qt's signal-slot connections (across thread boundaries)?

In Qt one can:
connect(object, &Object::someSignal, objectInAnotherThread, &Object::someSlot);
So, when I connect a signal from an object in a thread to an object in another thread, Qt queues the signal and someSlot will be executed in the thread of objectInAnotherThread.
This particular feature is very handy and safe, although could copy data.
Lambdas in C++11 are handy, but when replacing this kind of connection with a pure lambda callback (without Qt), the lambda will be executed in the thread of the caller. This will then usually require mutexes etc error-prone logic to make things right.
I'm aware of Boost::signals2 etc, but AFAIK they don't provide this same Qt-like behavior when used across thread boundaries..?
If I'd like to remove Qt for a reason or another, what are my options for drop-in replacement regarding my signal-slot connections?
What’s wrong with spinning up a thread and sending wrapped function calls to a queue that the thread pulls from and executes? The event queue in Qt is not very special other than it uses the “native” event loop. There’s no need to do that, though, and e.g. QtConcurrent::run threads implement a simple mutex+wait condition protected queue. Whenever the new events are delivered, the thread gets woken up and processes them until the queue is empty. The events can carry functor calls. In fact, the events can simply be std::function. The only sticking point is timers, which you’d have to implement on top of the primitive that waits on the wait condition. Those waits have timeouts, and you’d use a sorted timeout queue and schedule wake ups whenever a timer object should “tick”. This has the benefit of not using up any native timers and can potentially perform better.

Notify caller that a thread has finished

I am trying to use the multithreading features in the C++11 standard library and have the following situation envisioned.
I have a parent class which maintains a queue of thread. So something like:
std::queue<MyMTObject *> _my_threads;
The class MyMTObject contains the std::thread object.
The queue has a fixed size of 5 and the class initially starts with the queue being full.
As I have jobs to process I launch threads and I remove them from the queue. What I would like is to get a notification when the job is finished along with the pointer to the MyMTObject, so that I can reinsert them into the queue and make them available again.
I have basically 2 questions:
1: Is this a sound idea? I know I have not specified specifics but broadly speaking. I will, of course, control all access to the queue with a mutex.
2: Is there a way to implement this notification mechanism without using external libraries like Qt or boost.
For duplicates, I did look on the site but could not find anything that was suitable to manage a collection of threads.
I'm not sure if I need to mention this, but std::thread objects can't be re-used. Generally, the only reason you keep a std::thread reference is to std::thread::join the thread. If you don't plan to join the thread later (e.g. dispatch to threads and wait for completion), it's generally advised to std::thread::detach it.
If you're trying to keep threads for a thread pool, it's probably easier to have each thread block on the std::queue and pull objects from the queue to work on. This is relatively easy to implement using a std::mutex and a std::condition_variable. It generally gives good throughput, but to get finer control over scheduling you can do things like keep a seperate std::queue for each thread.
Detaching the threads and creating a work queue also has the added benefit that it avoids redundantly requesting the operating system create new threads which adds overhead and increases overall resource usage.
You could try to deploy some version of Reactor pattern I think. So, you could start one additional control thread that cleans after these workers. Now, you create a ThreadSafeQueue that will be used to communicate events from worker threads to control thread. This queue should be implemented in such a way that you can select on it and wait for any activity on the other end (some thread terminates and calls queue.push for example).
All in all I think it's quite elegant solution. I does add an overhead of an additional thread, but this thread will be mostly sleeping and waking up only once a while to clean up after the worker.
There is no elegant way to do this in Posix, and C++ threading model is almost a thin wrapper on Posix.
You can join a specific thread (one at a time), or you can wait on futures - again, one future at a time.
The best you can do to avoid looping is to employ a conditional variable, and make all threads singal on it (as well as indicating which one just exited by setting some sort of per-thread flag) just before they are about to exit. The 'reaper' would notice the signal and check the flags.
The issue is that this solution requires thread cooperation. But I know not of any better.

pthreads: perform function on main thread

I'm looking for the analogous of Cocoa's
-[NSObject performSelectorOnMainThread: withObject: waitUntilDone:]
method.
So basically I have a function that does some work on a separate thread but it must perform some synchronous calls that need to be performed on the main one.
in cocoa, the message is added to the run loop, which is cleared as part of its iteration.
to simulate this:
you'll a want a run loop
an abstract message system
and a reference counting mechanism (in most cases)
a way to add those messages to a run loop for scheduled execution
timers would be a nice addition
to accomplish something similar using pthread interfaces exclusively, start by reading up on conditions pthread_cond_t.
i know of no pthread interface with a 1-1 relationship for what you're trying to accomplish. conditions also operate without run loops, so you may need to bring that to the table, if you do not reuse a run loop implementation. if you use run loops, then you just need a lock to add messages to a thread with a run loop.
pthreads are a very low-level abstraction, so there's no easy way to do this with raw pthreads. Typically you'll want to write to a file descriptor to wake up an event loop on the main thread, then pass it a pointer to the function you want to run. You could even write pointer values onto a pipe(), then have the main thread execute them.
To wait synchronously, you can simply have a mutex and condition variable, plus completion flag on these execution request objects. Have the child thread wait on the mutex/condvar/completion flag, then in the main thread (under the mutex) set the flag and signal the cvar. Cleanup of the request structure would be done in the child.
To be more specific, it'd help if you could mention what event loop you have running on your main thread.

How can I pass data from a thread to the parent process?

I have a main process that uses a single thread library and I can only the library functions from the main process. I have a thread spawned by the parent process that puts info it receives from the network into a queue.
I need to able to tell the main process that something is on the queue. Then it can access the queue and process the objects. The thread cannot process those objects because the library can only be called by one process.
I guess I need to use pipes and signals. I also read from various newsgroups that I need to use a 'self-trick' pipe.
How should this scenario be implemented?
A more specific case of the following post:
How can unix pipes be used between main process and thread?
Why not use a simple FIFO (named pipe)? The main process will automatically block until it can read something.
If it shouldn't block, it must be possible to poll instead, but maybe it will suck CPU. There probably exists an efficient library for this purpose.
I wouldn't recommend using signals because they are easy to get wrong. If you want to use them anyway, the easiest way I've found is:
Mask all signals in every thread,
A special thread handles signals with sigwait(). It may have to wake up another thread which will handle the signal, e.g. using condition variables.
The advantage is that you don't have to worry anymore about which function is safe to call from the handler.
The "optimal" solution depends quite a bit on your concrete setup. Do you have one process with a main thread and a child thread or do you have one parent process and a child process? Which OS and which thread library do you use?
The reason for the last question is that the current C++03 standard has no notion of a 'thread'. This means in particular that whatever solution your OS and your thread library offer are platform specific. The most portable solutions will only hide these specifics from you in their implementation.
In particular, C++ has no notion of threads in its memory model, nor does it have a notion of atomic operations, synchronization, ordered memory accesses, race conditions etc.
Chances are, however, that whatever library you are using already provides a solution for your problem on your platform.
I highly suggest you used a thread-safe queue such as this one (article and source code). I have personally used it and it's very simple to use. The API consist in simple methods such as push(), try_pop(), wait_and_pop() and empty().
Note that it is based on Boost.Thread.