What's a calling thread? - multithreading

I am using CameraAPI2 of Android for app development.
I use the setOnImageAvailableListener(OnImageAvailableListener listener, Handler handler) to specify a callback or a listener.
The argument listener is a callback that will be run and the argument handler specifies the Thread the listener should be invoked on. If the handler is null, the listener should be invoked on the calling thread's looper.
The listener is an interface and its onImageAvailable() method should be overridden.
I am not quite clear what is the "calling thread". Is it the thread calling the setOnImageAvailableListener(...) method or the thread calling the onImageAvailable(...) method?

A calling thread is the thread that calls a method or the thread inside which a method is called. If thread1 calls method methodA (if methodA gets called from within thread1) then the calling thread of methodA is thread1. The listener argument specifies a callback method that will be called later in time. The calling thread will be the thread that calls the onImageAvailable method (the thread from which the call originated).
As per the official docs, the callbacks are delivered to the thread that makes the call to Camera.open.

Related

JVCL TJvThreadTimer.OnTimer : Is This Code Executed in The TJvThreadTimer, ie. NOT the main thread

I want to have the code executed by the OnTimer event to be executed in a separate (non-Main) background thread. this code does not access or communicate with the main thread/GUI. Simple question, I get the timer (TJvThreadTimer) is executed in it's own background thread, but:
Does the code contained in TJvThreadTimer.OnTimer event get executed in that background thread as well?
It is unclear from the limited documentation.
Thanks......
If you look at the timer's source code for yourself, you will see that the OnTimer event handler is called inside of a class method that is Synchronize()'ed by the internal background thread, which means the event handler runs in the main UI thread.

PostThreadMessage: Create a message queue

I have an issue concerning a Thread lacking a message queue at the begin of its life cycle. MSDN explains
The thread to which the message is posted must have created a message queue, or else the call to PostThreadMessage fails. Use one of the following methods to handle this situation:
(1) Call PostThreadMessage. If it fails, call the Sleep function and call PostThreadMessage again. Repeat until PostThreadMessage succeeds.
(2) Create an event object, then create the thread. Use the WaitForSingleObject function to wait for the event to be set to the signaled state before calling PostThreadMessage. In the thread to which the message will be posted, call PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE) to force the system to create the message queue. Set the event, to indicate that the thread is ready to receive posted messages.
Method (1) solves my issue, the second call to PostThreadMethod() always succeeds in my application.
However, I would like to comprehend the second method and simply don't understand "event object" (certainly not a normal Delphi event?) "to the signalled state" and "set the event to indicate".
QUESTION: Can someone please be so kind as to translate paragraph (2) into a short Delphi code example?
These event objects are synchronization objects, described in MSDN here: Event Objects.
At the bottom of that topic is a link to Using Event Objects which gives example code showing how to create events, set them, wait for them, etc.
In short you use the following functions:
CreateEvent to create the event objects.
CloseHandle to destroy it.
SetEvent and ResetEvent to set and reset the event object.
WaitForSingleObject to wait for it to be signaled.
You can use the TEvent class from the System.SyncObjs unit to wrap all of these low-level API calls. Then the process would become like so:
Create a TEvent object, Event say, in the reset state.
Create your worker thread, passing in Event.
Call Event.WaitFor in the manager thread to wait for the worker thread to signal that its message queue exists.
When the worker thread starts executing (i.e. at the start of its Execute method), have it create its message queue, and then set the event by calling Event.SetEvent.

QThread: Call child thread method from main thread

I have a thread created by inheriting QThread in which I called exec() to initiate the event loop. And this class that inherits QThread has a method in it.
How can I call that method from the main thread for it to execute in the child thread?
I assume that the execution of that method has to be queued in the child thread's event loop, so calling threadObject->childThreadMethod() won't be a good idea.
Is there any solution to this?
You can not call every member functions of the thread, but only slots and Q_INVOKABLE methods.
Use QMetaObject::invokeMethod() to call such a method, and make sure to set the connection type to Qt::QueuedConnection.
That way, the method will be called in the second thread whenever the control returns to the event loop in the second thread. The caller, i.e. the main thread, will continue immediately, so you won't be able to get a return value from the called method.
Behind the scenes, QMetaObject::invokeMethod adds a MetaCallEvent to the second thread's event queue.
Alternatively, create a signal/slot connection, and then emit a signal whenever you want the slot to be called in the other thread.
to run some function in a separate thread you can use QtConcurrent::run (i use it with QFutureWatcher). To run it every 5 or so seconds, use QElapsedTimer class
QFuture<void> future = QtConcurrent::run(this, &MyClass::foo2, ...foo2 arguments);
http://qt-project.org/doc/qt-4.8/qtconcurrentrun.html#run or check it here https://stackoverflow.com/search?q=QtConcurrent%3A%3Arun
or you can subclass QThread, reimplement QThread::run() method with the stuff you want to happen in your thread, and then create an instance of your thread and call start() on it.

how to detect when a thread has completed execution in blackberry?

How can I detect when the below code/thread has completed execution in Blackberry:
UiApplication.getUiApplication().invokeAndWait(new Runnable() {
public void run() {
EventInjector.KeyEvent inject = new EventInjector.KeyEvent
(EventInjector.KeyEvent.KEY_DOWN,Characters.ENTER, 0);
inject.post();
inject.post();
}
});
Can I get a notification when this thread has executed so that I can start another thread?
Thanks in advance.
Typically, the join() method is used to determine when a thread has finished. Calling join() typically blocks, so use it with care.
Here is a reference.
You need a synchronized block, here is a short description of how it works.
The "sleeping" thread must lock an object, and call wait() on it.
The other thread locks the same object, and sends a notify() or notifyAll() to wake up the first one.
If you start the thread explicitly (not in this case), you can simply call join() on it, and synchronously wait for it to finish.
- Edit: using join() on a thread
Assuming you already have a runnable object:
Thread myThread = new Thread(myRunnable);
myThread.start();
doOtherStuff();
myThread.join();
But if something goes wrong, you are stuck on the join() call, because BlackBerry has no timeout for this call.

How to know when CloseThreadPool() function completes?

i am new to Sockets programming and going through the Documentation.
From a documentation i found about CloseThreadPool() function :
CloseThreadpool function. The thread pool is closed immediately if there are no outstanding callback objects that are bound to the thread pool. If there are, then the thread pool is released asynchronously when those outstanding objects are freed.
This thread pool is in a thread itself. my main thread takes input for exit. if exit is inputted i set global variable KEEP_LISTENEING to false.
How would i wait my main thread to stop/sleep untill this function truly completes in another thread ?
Use a cleanup group to wait for all callbacks. The sequence is:
CreateThreadpoolCleanupGroup()
SetThreadpoolCallbackCleanupGroup(&CallbackEnvironment, pointerCleanup, ...)
CloseThreadpoolCleanupGroupMembers(, FALSE, )
CloseThreadpoolCleanupGroup()

Resources