Please explain Wx::Thread example - multithreading

The documentation for Wx::Thread includes a small but instructive example of how to post an event from a child thread to a window in the main or parent thread.
What I cannot understand is how it can possibly be valid to refer to the file-scoped lexical variable $frame from the child thread as illustrated. The sequence of events is as follows: 1) start child thread; 2) initialize $frame; 3) ...time passes...; 4) child thread posts event to $frame.
I had thought that all non-shared variables get cloned when a thread starts. How can $frame be anything but undef in the child thread?

Related

How to synchronize killing of child Actor using setReceiveTimeout in Akka

Background:
I am using C#, but I think this question applies to Java and Scala as well. I have one master actor that passes along all "Job Messages". These jobs can be executed at the same time, as long as they don't share don't share a "concurrency id". To take care of this, I was spawning one child actor for each "concurrency id". I then used setReceiveTimeout to clean up the children when they've been idle.
What I can't figure out is how I can do this without a race condition. Here are a couple of ways that don't work:
1) Remove the child from the Dictionary, and tell the child to terminate. This does not work because messages might have been added to the child's queue between the time the timeout was fired, and before the parent started processing the message, resulting in messages being lost.
2) Remove the child from the Dictionary, and send a PoisonPill. This does not work because the child might continue to process new work. If more work reaches the parent, the parent would create a new child, and the means two thing with the same concurrency id are running at the same time.
3) Remove the child from the dictionary, and "ask" the child with a message. If the message returns
Is there any way the parent can ask the child if there are any messages in the child's queue? (This would not be a race condition, because all messages to the child come from the parent)
Can the parent check if the child's queue is empty, and the child isn't processing anything?
Should I add a new message and "ask" the child if it's done? (This would be safe because I know the parent is the only one who sends messages, but there's a moderate chance this might block if the child is processing a message, or if the there's no threads available in the dispatcher's pool.
My question is similar to this question, but I'm adding the additional constraint of the "concurrency id", and not worried about "zombie actors" who are currently shutting down, as long as the zombies have no work and won't get more work:
Get or create child Akka actor and ensure liveness
Akka makes sure that actor mailboxes are private. The mailbox of one actor is no business of another actor. A parent checking if the child's mailbox is empty would be wrong.
Why do you think ask would block?
One option: do not kill the children. If you have a fixed set of concurrency IDs you use over and over again and again, just keep all the actors alive. Actors do not consume too much resources.
Another option:
When the parent wants to kill the child:
send a PoisonPill to the child and remove him from the Dictionary.
When the parent receives a "job message" with concurrencyID:
if (Dictionary.contains(concurrencyID) {
send message to child
} else {
if (parent has child with name concurrencyID) {
delay message - for example with scheduler // child is terminating
} else {
create a child with name concurrencyID
send message to child
}
}

How can I ensure that NSOperationQueue uses only one thread?

ABAddressBookRef can only be accessed by one thread. I think a good model would be having a thread for ABAddressBookRef in the background, besides the main thread.
How can I ensure that there's only one thread while using NSOperationQueue? Simply setting max concurrency to 1 won't guarantee it to be run on the same thread.
Should I use other unique threads like web threads?
You can manually create a thread and redirect all address book access to it.
You create a thread with something like this (adapted from documentation):
NSThread* myThread = [[NSThread alloc] initWithTarget:[MyThread new]
selector:#selector(myThreadMainMethod)
object:nil];
[myThread start]; // Actually create the thread
Note that for the thread to be useful, you have to implement a run loop in thread's main method.
See example implementation of run loop in this answer.
You are then able to do stuff on this thread using the NSObject's method performSelector:onThread:withObject:waitUntilDone:.
Here's a wrapper library for ABAddressBookRef that implements this concept – RHAddressBook.

Allow a child thread to call a delegate to the parent when the parent is in a join state waiting on the child in c#

I have a form that is responsible for creating and setting up an instance of an object, and then telling the object to go do its work. The process is a long one, so there's an area on the form where status messages appears to let the user know something is happening. Messages are set with a setMessage(string msg) function. To allow the form to remain responsive to events, I create a new thread for the object to run in, and pass it the setMessage function as a delegate to allow the object to set status messages on the form. This part is working properly. The main form is responsive and messages posted to its setMessage function appear as expected.
Because the process is a long one, and is made up of many steps, I want to allow the user to terminate the process before it's finished. To do this I created a volatile bool called _stopRequested and a function called shouldStop() that returns its value. This is also given to the object as a delegate. The object can tell if it should terminate by checking shouldStop() periodically, and if it's true, shut down gracefully.
Lastly, Windows controls are not thread safe, so the compiler will complain if a thread other than the one that created the control tries to manipulate it. Therefore, the setMessage function is wrapped in an if statement that tests for this and invokes the function using the parent thread if it's being called from the worker thread (see http://msdn.microsoft.com/en-us/library/ms171728(v=vs.80).aspx for a description).
The problem arises when the user requests a shutdown. The main form sets _stopRequested to true and then waits for the child thread to finish before closing the application. It does this by executing _child.Join(). Now the parent thread (the one running the form) is in a Join state and can't do anything. The child thread (running the long process) detects the stop flag and attempts to shut down, but before it does, it posts a status message by calling it's setMessage delegate. That delegate points back to the main form, which figures out that the thread setting the message (child) is different than the thread that created the control (parent) and invokes the function in the parent thread. The parent thread is, of course, in a Join state and won't set the text on the text box until the child thread terminates. The child thread won't terminate because it's waiting for the delegate it called to return. Instant deadlock.
I've found examples of signaling a thread to terminate, and I've found examples of child threads sending messages to the parent thread, but I can't find any examples of both things happening at the same time. Can someone give me some pointers on how to avoid this deadlock? Specifically, I'd like the form to wait until the child thread terminates before closing the application but remain able to do work while it waits.
Thanks in advance for the advice.
1-(lazy) Dispatch the method from a new Thread so it doesn't lock
2-(re-think) The main UI thread should be able to control the child thread, so forget the _stopRequested and shouldStop() and implement a childThread.Abort() , abort does not kill the thread, but sends a ThreadAbortException
which can be handled or even canceled
catch(ThreadAbortException e)
{
ReleaseResources();
}
Make the ReleaseResources safe by making various checks such as:
resource != null
or
resource.IsClosed()
The ReleaseResources should be called normally without abort and also by abort.
3-(if possible)stop the child, via main thread call ReleaseResources()
You may have to implement a mix of these.

unix fork() understanding

int main(){
fork();
}
I know this is a newbie question, but my understanding is that the parent process now will fork a new child process exactly as the parent one, which means that the child should also fork a child process and so on... In reality, this only generates one child process. I cant understand what code will the child be executing?
The child process begins executing at the exact point where the last one left off - after the fork statement. If you wanted to fork forever, you'd have to put it in a while loop.
As everybody mentioned, the child also starts executing after fork() has finished. Thus, it doesn't call fork again.
You could see it clearly in the very common usage like this:
int main()
{
if (fork())
{
// you are in parent. The return value of fork was the pid of the child
// here you can do stuff and perhaps eventually `wait` on the child
}
else
{
// you are in the child. The return value of fork was 0
// you may often see here an `exec*` command
}
}
You missed a semi-colon.
But the child (and also the parent) is continuing just after the fork happenned. From the point of view of application programming, fork (like all system calls) is "atomic".
The only difference between the two processes (which after the fork have conceptually separate memory spaces) is the result of the fork.
If the child went on to call fork, the child would have two forks (the one that created it and the one that it then made) while the parent would only have one (the one that gave it a child). The nature of fork is that one process calls it and two processes return from it.

Thread Pool : how to spawn a child task from a running task?

A simple thread pool with a global shared queue of tasks (functors).
Each worker (thread) will pick up one task from the worker, and execute it. It wont execute the next task, until this one is finished.
Lets imagine a big task that needs to spawn child tasks to produce some data, and then continue with evaluation (for example, to sort a big array before save to disk).
pseudo code of the task code:
do some stuff
generate a list of child tasks
threadpool.spawn (child tasks)
wait until they were executed
continue my task
The problem is that the worker will dead lock, because the task is waiting for the child task, and the thread pool is waiting for the parent task to end, before running the child one.
One idea is to run the child task inside the spawn code:
threadpool.spawn pseudo code:
threadpool.push (tasks)
while (not all incoming task were executed)
t = threadpool.pop()
t.run()
return (and continue executing parent task)
but, how can I know that all the task were executed , in an efficient way?
Another idea is to split the parent task.. something like this:
task pseudo code:
l = generate a list of child tasks
threadpool.push ( l , high priority )
t = create a task to work with generated data
threadpool.push (t , lo priority )
But i found this quite intrusive...
any opinions?
pd. merry christmas!
pd2. edited some bad names
You can have a mechanism for the children threads to signal back to the main worker whenever they are done so it can proceed. In Java, Callable tasks submitted to an ExecutorService thread pool respond back with their results as Futures data structures. Another approach would be to maintain a separate completion signal, something similar to a CountDownLatch, which will serve as a common countdown mechanism to be updated every time a thread completes.

Resources