Skip to content
Advertisement

Distribute Range of Numbers between each threads

Config File

JavaScript

I have a config file above in which I have number of threads I want to use and the client instance is able to use ID range from 1 to 1000 and suppose the client threads is set at 10, so each thread would have range of 100 id’s(basically by dividing end range with thread size) that it can use without stepping on other threads. so What I want is that each thread should use 100 id’s from that range without stepping on other threads- for example

JavaScript

I know how to write a multithreading program, but not sure how should I divide the range between various threads.

JavaScript

Advertisement

Answer

Each thread gets N = (EndRange - StartRange + 1) / ThreadSize numbers.

Thread number i gets range (StartRange + i*N) - (StartRange + i*N + N - 1).

In your example N = (1000 - 1 + 1) / 10 = 100.

Thread i = 0 would get range (1 + 0*100) - (1 + 0*100 + 100 - 1) = 1 - 100

Thread i = 1 would get range (1 + 1*100) - (1 + 1*100 + 100 - 1) = 101 - 200

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement