World's most popular travel blog for travel bloggers.

Timing threads in an algorithm

, , No Comments
Problem Detail: 

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?

If more info is needed, please post

Asked By : user3129956
Answered By : Wandering Logic

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.

Best Answer from StackOverflow

Question Source :

3200 people like this

 Download Related Notes/Documents


Post a Comment

Let us know your responses and feedback