1

My objective is to create a thread that is reusable and not one that terminates when reaching the end of the thread function.

This is a pseudo code of what I'm trying to achieve with pthreads:

bool doRun = true;

void thread_func( void* p ) {
    while( doRun ) {
        waitForSignal() // just like waitForSingleObject
        // run thread
    }
}

I used the win32 function waitForSingleObject together with an Event in the past quite a lot and I have no idea how to implement that kind of behavior with pthreads.

Your help is appreciated

RonTLV
  • 2,376
  • 2
  • 24
  • 38
  • 3
    You're probably looking for [`pthread_cond_wait()`](https://linux.die.net/man/3/pthread_cond_wait). –  Jul 26 '17 at 11:39
  • 1
    Which OS please? – alk Jul 26 '17 at 11:39
  • @alk the question mentions *pthreads* ... –  Jul 26 '17 at 11:40
  • @alk android jni c – RonTLV Jul 26 '17 at 11:40
  • @FelixPalmen: There is a POSIX thread port to windows. – alk Jul 26 '17 at 11:40
  • @alk sure, but I think it's straight forward to suggest the POSIX threads method of waiting for a condition when the question asks about *pthreads* ;) (and we can probably exclude win32 anyways) –  Jul 26 '17 at 11:42
  • It seems you are using "signal " in a somewhat ambiguous manner, right? – alk Jul 26 '17 at 11:42
  • @FelixPalmen: Well, the title does not mention "condition" but "signal" ... – alk Jul 26 '17 at 11:42
  • 1
    @RonTLV, do you mean *signal* as in `SIGINT`? – Florian Weimer Jul 26 '17 at 11:42
  • @alk which is an abstract "signal to continue", as [`pthread_cond_signal()`](https://linux.die.net/man/3/pthread_cond_signal) would issue. I think this is quite obvious from the context of the question. –  Jul 26 '17 at 11:44
  • What I need is the exact behavior of waitForSingleObject( Event ) / m_event.pulseEvent(), or Java wait / notify, or Java count down latch. – RonTLV Jul 26 '17 at 11:46
  • @RonTLV that could be a problem in general, since those synchro mechanisms have 'issues', and replicating those issues will be difficult:) I would use a semaphore, but maybe that's just me.. – Martin James Jul 26 '17 at 11:59
  • 3
    See this answer: https://stackoverflow.com/a/33116597/6608866 – PaulR Jul 26 '17 at 12:05

0 Answers0