Thread: notify main when a threa is done

  1. #1
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694

    notify main when a threa is done

    I am serfing the net for 2 hours and I couldn't find the answer.
    I am using pthread.
    I have jobs to run coming into and I push_back them in a vector and init them.
    I want every time that a job comes into to check if some of the other threads is done. All the threads are **not** detached.
    I want to clear the jobs that are already done, but if I call wait(), then it will stop the parallel execution!

    How can I do that? I am a total beginner in all this!
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    If you would explain more you'd get better responses.

    That said, you sound as if you are approaching the task from the wrong direction.

    Do not ask a bunch of threads for "Are you done?"; have each thread say "I'm done." and record that information.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    >have each thread say "I'm done." and record that information.
    That's exaclty what I want! But I do not know how to do it!

    Next time I will be more analytical!

    EDIT:
    >you are approaching the task from the wrong direction.
    Maybe, but that's in the process of learning I guess.
    Last edited by std10093; 01-07-2014 at 06:26 PM.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    If you spawn 4 threads and then wait on them in the original thread, the spawned threads will continue to execute.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Yes, but by the time I call join() (that is what you mean by wait I guess), main() will be blocked, in order the thread to finish.
    I do not to block main, until I have received all the jobs I have to schedule. Is that wrong?
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by std10093 View Post
    by the time I call join() (that is what you mean by wait I guess), main() will be blocked, in order the thread to finish.
    "wait" was your word.
    Yes, main blocks until a thread ends.

    Quote Originally Posted by std10093 View Post
    I do not to block main, until I have received all the jobs I have to schedule. Is that wrong?
    Then "schedule" your jobs before calling join. What's the problem?

    You need to give a complete description of this program because I (and I think others) can't really understand what you want.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  7. #7
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Ok, let me take advantage of your answers and re-state.

    Let's say I have 10 Jobs to schedule. For some reason, we can run in parallel only two jobs!
    The 10 jobs are created and call the schedule() of Sheduler object inside the loop, thus they will come really quickly to the scheduler, of course requesting to be executed. The creation of the job isn't a heavy task.
    When a job comes, if 0 or 1 jobs are executing, I can create a new thread. If 2 (which for this example is the max number of threads that can run in parallel) jobs run, then I insert the "waiting to be served" jobs in a FIFO queue for example.
    When one or more jobs are done, I want them to say "Hey Samaras, I am done!", collect the results of them and execute the pending jobs.

    In my example, the max number of threads is 2. So, I think that it would be ok to do somehting like this:
    Code:
    if(threads_running == MAX_THREADS)
       join_all_threads();
    With MAX_THREADS a small number I think this isn't wasting resources. That's my fear! That I will have some slots busy by jobs that are already done and could be replaced by jobs ready to work.
    If MAX_THREADS are ten and the job is "big", then I shouldn't wait for all the threads to join(). The first one may have executed, while the other 9 are running. I would like the first one to say somewhere or to me that it is done.

    I think the goal is what phantomap said: have each thread say "I'm done." and record that information.

    [EDIT]
    4 hours to get up to go to uni.goodnight. Will check back tomorrow.
    Last edited by std10093; 01-07-2014 at 07:09 PM.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  8. #8
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    I think the goal is what phantomap said: have each thread say "I'm done." and record that information.
    O_o

    Well, I was trying to get you facing the right direction.

    So, anyway, you are clearly trying to implement the "thread pool pattern" without the having the jargon under your belt.

    You shouldn't have a creating/joining/destroying process.

    I've told you what to search for, but here is a general overview:

    1): Create as many threads as you have lines of execution available.
    2): Add all new jobs to a queue.
    3): Wrap thread processes with a value registry.
    4): Wrap thread processes with some form commit step.

    Code:
    // ...
    int main()
    {
        // ...
        ThreadPool sPool;
        // ...
        sPool.addJob(/*Whatever*/); // behind the scenes: while(c < MAX_THREADS){CreateThread(Process, sPool);}
        // ...
        sPool.join();
        // use sPool.getResults();
    }
    // ...
    void Process // The actual implementation (lines of execution) of a "thread" does all the dirty work.
    (
        Pool & fPool
      , /**/
    )
    {
        // ...
        Job * sJob;
        while(sJob = fPool.popJob()) // get a new job (commit step) from fPool.mJobs (job queue)
        {
            // wait on the job to finish and store the result 
            fPool.appendResult(sJob()); // fPool.mResults (value registry)
        }
        // API cleanup
        // ...
    }
    With such a strategy, corrected for reality of course, each job says "I'm Done!"; the actual threads, from system/hardware API, don't stop and are not cleaned up until all jobs are finished.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    If you want the main() thread to poll in order to check for termination of a thread (e.g. check if the thread is terminated, while doing something else) you have two general choices.

    The first is to use non-standard functions (e.g. pthread_tryjoin_np() under reasonably recent versions of gnu libraries). The catch is that this doesn't work with all compilers, libraries (glibc), or host systems.

    The other way requires cooperation of the thread you wish to check for. The simplest is setting some state (protected by a mutex) in the thread you want to poll for, and poll for the change of state in your main() function. The advantage is that it is portable (at least to posix) and can be extended to keep track of an arbitrary number of threads (e.g. maintain a map of thread ID to information about whether that thread reports itself terminated within the critical section). The disadvantages are that it it is not "built in" - it is easy for a thread to forget to register that it has finished, it is also possible for a thread to register that it has finished prematurely, and it is a polling mechanism (with all limitations that apply).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  10. #10
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    >you are clearly trying to implement the "thread pool pattern" without the having the jargon under your belt.
    Google says yes!

    At the way phantomap points out, I can not see how the thread can notify me.
    I was thinking of the way that grumpy said (the portable one), from the start, but -as now-I do not know how to do that. It would be nice to have some shared variable between a thread and main, where it would be initialized in -1, thread will set it a 0 when its done and the main would read that variable. That is the state that grumpy says, pretty much. I am not aware of the tools however, needed to make this possible!
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  11. #11
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    I feel I shoudn't do that with mutex variables.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  12. #12
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    I had an idea on the metro.
    Fetch a pending job, when a thread says "I am done". Internet shows my some conditional variables..Am I looking at the correct direction?Any tips?
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  13. #13
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by std10093 View Post
    I feel I shoudn't do that with mutex variables.
    Why? Mutexes aren't the only means of doing this, but the general requirement (synchronise access to the information being shared) is necessary. You might get lucky on some platforms, but a potential consequence of partially completed operations (one that writes to particular data and one that reads it) preempting each other is data poisoning within a program.

    You could also use atomic variable access (for example, gcc builtins in recent versions of gcc, or functions like InterlockedIncrement() in the win32 API) . The catch with these techniques that is they falls in the category of using non-standard or platform specific functions.

    Quote Originally Posted by std10093 View Post
    Internet shows my some conditional variables..Am I looking at the correct direction?Any tips?
    Condition variables are a means of avoiding polling, but they do cause the waiting thread to block unless it times out. Have a look here, for example. Using them requires use of a mutex though.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  14. #14
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    So the way to go is using conditional with mutex variables. I hope. I will try them in 2-3 hours.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  15. #15
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Given that your problem description is both contradictory (you simultaneously described wanting to poll "I want every time that a job comes into to check if some of the other threads is done", at the same time as saying you don't want polling) and vague, it's quite possible anything or nothing will be the way to go.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 06-01-2011, 03:08 AM
  2. Replies: 3
    Last Post: 02-06-2011, 08:41 AM
  3. Replies: 3
    Last Post: 09-11-2008, 12:29 AM
  4. Notify Icon balloon tip time
    By Rune Hunter in forum C# Programming
    Replies: 6
    Last Post: 07-12-2005, 09:12 AM
  5. how to notify the EOF?
    By Jasonymk in forum C++ Programming
    Replies: 1
    Last Post: 01-06-2003, 01:31 AM