-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocumentation.txt
32 lines (20 loc) · 1.26 KB
/
documentation.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
Algorithm used :
Sort the sleep timings.
Start them in separate thread.
Now first thread will finish first as we have already sorted,so wait for it to finsh and when it finishes ,start a new thread with sleeping time of new sleeping time - sleep already taken.
For example
async->async_wait(8000, &callback);
async->async_wait(3000, &callback);
async->async_wait(4000, &callback);
async->async_wait(1000, &callback);
async->async_wait(2000, &callback);
async->async_wait(5000, &callback);
async->async_wait(9000, &callback);
Suppose max thread allowed are 2.
Initialize the buffer with these sleep timings and sort the buffer array.
Start two threads which will wait for 1000 ms and 2000 ms.
Now program waits until sleep time of 1000 ms finshes and when it finishes it creates a new thread with sleep timing of 3000 - 1000 = 2000 ms as 1000 ms sleep already taken.
Now next request of 5000 ms will be taken when the thread running 2000 ms finishes,then it finishes it will start a new thread with thread timing of 5000 - 2000 = 3000 ms as 2000 ms sleep already taken.
So at a given time all the thread will be in running state,and if no of request are less than no of max threads then only that no of threads will be created.
Thanks,
Tarun Prakash Singh(TPS)