Comments (1)
(Trying to help the maintainers clear out some issues...)
Why do you think your output is unexpected? Your sleep times are between 1 and 2 seconds, so (usually) the next thing that will happen after a task returns is a new task starting on the same thread that just completed its previous task. This is exactly what your output shows.
To see it more clearly, replace your task
function with the following:
#include <sys/time.h>
double startTime;
double wallTime(void) {
struct timeval tv;
gettimeofday(&tv,NULL);
return tv.tv_sec + tv.tv_usec/1e6 - startTime;
}
void task(void *arg){
int sleep_time = rand()%1000000+1000000;
printf("%5.2f: Thread #%p working on %3ld (%.2f)\n",
wallTime(), (void *)pthread_self(), (intptr_t)arg, sleep_time/1e6);
usleep(sleep_time); // intensive task
printf("%5.2f: Thread #%p finished on %2ld (%.2f)\n",
wallTime(), (void *)pthread_self(), (intptr_t)arg, sleep_time/1e6);
}
Everything is completely hunky dory:
0.00: Thread #0x700001bd6000 working on 0 (1.87) <-- 1.87 sec task starts on thread 0x700001bd6000
0.00: Thread #0x7000018c4000 working on 2 (1.59)
0.00: Thread #0x700001c59000 working on 3 (1.64)
0.00: Thread #0x700001a4d000 working on 1 (1.26)
0.00: Thread #0x700002177000 working on 6 (1.53)
0.00: Thread #0x700001cdc000 working on 4 (1.74)
0.00: Thread #0x7000019ca000 working on 8 (1.03)
0.00: Thread #0x700001d5f000 working on 10 (1.63)
0.00: Thread #0x700001b53000 working on 9 (1.97)
0.00: Thread #0x700001ad0000 working on 7 (1.32)
.
.
.
1.64: Thread #0x700001c59000 working on 34 (1.26)
1.75: Thread #0x700001cdc000 finished on 4 (1.74)
1.75: Thread #0x700001cdc000 working on 35 (1.83)
1.78: Thread #0x7000020f4000 finished on 11 (1.78)
1.78: Thread #0x7000020f4000 working on 36 (1.67)
1.86: Thread #0x700001f6b000 finished on 17 (1.86)
1.86: Thread #0x700001f6b000 working on 37 (1.64)
1.87: Thread #0x700001bd6000 finished on 0 (1.87) <-- 1.87 sec task finishes on thread 0x700001bd6000
1.87: Thread #`0x700001bd6000 working on 38 (1.81) <-- new (1.81 sec) task starts immediately on this thread
.
.
.
3.58: Thread #0x700001cdc000 working on 58 (1.91)
3.67: Thread #0x700001947000 finished on 41 (1.41)
3.67: Thread #0x700001947000 working on 59 (1.70)
3.68: Thread #0x700001bd6000 finished on 38 (1.81) <-- 1.81 sec task finishes at 1.87+1.81 = 3.68 elapsed seconds
3.68: Thread #0x700001bd6000 working on 60 (1.20)
3.88: Thread #0x700001e65000 finished on 43 (1.20)
3.88: Thread #0x700001e65000 working on 61 (1.30)
from c-thread-pool.
Related Issues (20)
- If a child thread holding a mutex is terminated by "thpool_destroy(thpool)", what will happen to the mutex?
- example.c: fix a warning HOT 1
- Support for a priority queue for the jobqueue.
- support to zephyr RTOS HOT 1
- Can I pass multiple parameters to the thpool_add_work() method? HOT 2
- Why is `threads_keepalive` a global variable? HOT 1
- why do you define bsem *has_jobs; ? HOT 1
- When a thread is in an endless loop, can it not exit and be destroyed? HOT 1
- Reinitialization of mutex and cond in bsem_reset is a bug
- Why don't threadpool lock num_threads_working? HOT 1
- Does not improve runtime while using this threadpool implementation
- Is it possible to wait for a specific task?
- Doesn't release memory until job queue is empty? HOT 1
- thpool.c has leftovers after some merge HOT 1
- question about threads_keepalive variable. HOT 1
- 'SIGUSR1' undeclared HOT 1
- Errors when compiling with C++
- Any question weclomed in https://github.com/52coder/C-Thread-Pool
- The execution result seems to be something wrong while I use the Thread pool? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from c-thread-pool.