Code Monkey home page Code Monkey logo

Comments (1)

swofford avatar swofford commented on July 17, 2024

(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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.