Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This question relates to timing threads to go at a very precise rate. Consider t

ID: 652012 • Letter: T

Question

This question relates to timing threads to go at a very precise rate. Consider the following scenario; A program creates a series of threads that continually add a random set of elements to their local storage. The program finishes and returns the thread that received the shortest number of elements in its random set. If one thread was to go faster than another, the program would be incorrect. Is there a way to "lock" the threads to go at the same speed? If so would it be possible for someone post a quick python or other language script that performs this task?

Explanation / Answer

You are probably looking for a barrier synchronization primitive. Each thread calls into the barrier when it is done creating its set. Only when all the threads have called into the barrier are any threads permitted to proceed.

If your programming language doesn't include a barrier primitive, then you can fake it, in your example, simply by having the main thread sit in a loop and join with all the children thread as the children finish and return from their computation. The loop should only exit when it sees every child thread finish.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote