Sleep thread for seconds


#1

Is there any way to do something like sleepThreadForSeconds(.5f)

Or maybe:

while( getTime() < t )
    currThread.sleep;

?

I want to play a sound, wait half a second, do something else, wait a second, do something else etc.

In C# there was the yield command.

Is there any way to do this in JUCE without either splitting my function into multiple functions and using timers, or something involving nesting lambdas and timers?

π

PS just found http://www.juce.com/forum/topic/how-wait-using-timer


#2

Using Thread class?

Are you using the Thread class? Look at the Thread::wait method.


#3

http://www.juce.com/juce/api/classThread.html#a17aca9c9609310f830298d6a0206d18d

^ That looks fantastic! Any reason not to use it?

I'm scratching my head because everyone on the forum seems to be saying 'use timer'.

I suppose I would need to pay attention to which thread I'm on, and probably spawn my own worker.

EDIT:

Got it working...


class Calib : Thread { 
    public: Calib() : Thread("CalibrationThread") { 
        startThread(); 
    }; 

    void run() override { 
        for (int i = 0; i < 8; i++) { 
            DBG(String(i)); 
            wait(500); 
        } 

        stopThread(0); // FAIL
        waitForThreadToExit(0); 
        delete(this); 
    } 
}; 

void calibrate() { 
    Calib* calib = new Calib(); 
}

... but it leaks upon exit.  So how to make it take care of removing itself?

 Maybe this is impossible?

 The cleanest way I can see is to have it send a message to the parent object indicating that it has completed. And the parent object removes it.  Or maybe it sends the message to itself...

EDIT: Gottit!


        void run() override
        {
            for (int i = 0; i < 8; i++) {
                DBG(String(i));
                wait(500);
            }
            struct Msg : CallbackMessage {
                Calib* t;
                Msg(Calib* _t) : CallbackMessage() {
                    t = _t;
                }
                void messageCallback() override {
                    t->stopThread(0);
                    t->waitForThreadToExit(0);
                    delete(t);
                }
            };
            Msg* m = new Msg(this);
            m->post();
        }

w00t WOoT!

π


#4

Or keep it simple:

struct WorkerThread  : private Thread,
                       private AsyncUpdater
{
    WorkerThread() : Thread ("xyz")    { startThread(); }
    ~WorkerThread() { stopThread (1000); }

    void run() override
    {
        //...do your work here, remembering to check threadShouldExit() regularly...

        triggerAsyncUpdate();
    }

    void handleAsyncUpdate() override
    {
        delete this;
    }
};

 


#5

Final code:

class Foo()
{
    class Calib : public Thread
    {
    public:
        Calib() : Thread("CalibrationThread") {
            startThread();
        };
    protected:
        void run() override {
            for (int i = 0; i < 8; i++) {
                DBG(String(i));
                wait(500);
            }
            struct Msg : CallbackMessage {
                Calib* t = nullptr;
                Msg(Calib* _t) : t(_t) { }
                void messageCallback() override {
                    t->stopThread(0);
                    t->waitForThreadToExit(0);
                    delete(t);
                }
            };
            (new Msg(this))->post();
        }
    };

    void calibrate() {
        new Calib();
    }

π


#6

Lush!

PS What's with the pretty dark-themed code box? It's a massive improvement! Can we all do that?

PPS Also the current widget is a bit broken for copy/paste.