I am an experienced C programmer who has never used threads or parallelism before. I've been reading about it, but I do not see an example of what I want.
I use the gcc C compiler on Mac and linux. I want to replace an important procedure X in my system with a new procedure X2 that will start up two methods, as two threads to be run on two different processors whenever the machine has multiple CPUs (most do these days).
These two methods may share a few global variables, but they will not write to any memory locations other than their own stacks. They will each call many other procedures in the system. I do not envision any other parallel processing.
As soon as either thread finishes, that's it! That's the answer. X2 should immediately kill the other thread and return the answer to whoever called X2.
Maybe I'm naive but I would think this is a well known use of threads. Example code please!
I propose to try the following program. It uses two Linux processes to run two procedures, created by a parent process. As soon as one child finishes, the parent gets notified and terminates the other. Studying the documentation for the system calls appearing in the include section is of course necessary to understand the code.
On my machine, the program produces the following output:
It is possible for the two processes to finish at the same time so that they both print their "
finished
" message, but even in this case the parent declares one of them to be the first, and terminates the other.