I have the following piece of code (C#): is it correct?
Thread[] threads = new Thread[totalThread];
for (int i=0; i<totalThread; i++) {
threads[i] = new Thread(new ThreadStart(Work));
}
sw.Start();
for (int j=0; j<num_threads; j++) {
threads[j].Start();
}
for (int k=0; k<num_threads; k++) {
threads[k].Join();
}
sw.Stop();
sw is a Stopwatch, work is some method. I would like to time this method, using a few threads, but the whole thing freezes (seems like it is not joining). I need to know if my thread handling is good, or if I’m screwing up. If the latter, it is obvious that my problem lies in the method I’m calling, rather than in thread mgmt.
It’s perfect as it is. I don’t see any problem other that the
num_threads/totalThread. Note that iftotalThreadis big (where with big I mean bigger than 1-2 for each CPU Core), you should probably use theThreadPool.