1

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!

  • 1
    Stackoverflow might be a more appropriate platform for this question. Quick advise: look into openMP, it lets you do thread programming without knowing much about threads and without much modification to existing code. – Ketan Maheshwari Feb 10 '14 at 21:51
  • I'm wondering whether this is more of a C question, and may not be suited to this Unlix&Linux Section, however also understand that the implementation in C really depends on the OS it is runing in. Think this is a good question so marking up. – X Tian Feb 10 '14 at 21:53
  • Strictly speaking, programming questions are [off topic](http://unix.stackexchange.com/help/on-topic) here with the exception of shell, perl and similar scripting languages that are used in the administration of *nix systems. In any case, even if it were on topic here, you have a _much_ better chance of getting a good answer from the [SO] crowd. Voting to close/migrate. – terdon Feb 10 '14 at 22:00
  • That's funny - I already commented on exactly the same question once.... – Martin James Feb 11 '14 at 13:50
  • Oh - there it is: http://stackoverflow.com/questions/21688464/ending-the-first-successful-thread-in-gcc-c-linux – Martin James Feb 11 '14 at 13:52

0 Answers0