Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix CEvent auto-reset behaviour in case of broadcast signal. #4

Closed
wants to merge 1 commit into from

Conversation

srg70
Copy link

@srg70 srg70 commented Oct 25, 2018

I probably found some minor issue with CEvent in auto-reset mode. When event becomes signalled by Broadcast() method, and some thread been waiting for event in a loop with timeout, the event remains signalled after successful checking of signal state. I.e. no automatic reset done.

The issue was with a counter of waiting threads, that was not decremented, because of compiler optimisation of boolean expression.

There is no issue when Signal() method is using.

Following is small test that fails before changes and succeeded after.

#include <iostream>
#include "threads/threads.h"

using namespace P8PLATFORM;

class bg_thread : public CThread
{
public:
    bg_thread(CEvent& e) : event(e)
    {}
    virtual void *Process(void)
    {
        bool isSignaled;
        do{
             isSignaled = event.Wait(1*1000);
            if(!isSignaled)
                std::cout << "Waited 1 sec. No signal\n";
            else
                std::cout << "Got signal!\n";
        }while(!isSignaled/* && !IsStopped()*/);
        std::cout << "Thread exit\n";

        return nullptr;
    }
private:
    CEvent& event;
};


int main(int argc, const char * argv[]) {
    // insert code here...
    CEvent event;
    bg_thread thread (event);
    thread.CreateThread();
    std::cout << "Will sleep 5 sec\n";
    CEvent::Sleep(5*1000);
    std::cout << "Signal event\n";
    //event.Signal();
    event.Broadcast();
    thread.StopThread();
    std::cout << "Thread stopped\n";
    if(event.Wait(100))
        std::cout << "Event is still in signal state!\n";
    else
        std::cout << "Timeout. OK, event is NOT signaled.\n";
    return 0;
}

@srg70
Copy link
Author

srg70 commented Nov 6, 2018

PR been merged to Pulse-Eight/platform repo.

@srg70 srg70 closed this Nov 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant