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. #37

Closed
wants to merge 8 commits into from

Conversation

srg70
Copy link
Contributor

@srg70 srg70 commented Aug 7, 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 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;
}

@opdenkamp
Copy link
Contributor

nice find! could you rebase this, then I'll merge it in. thanks

* P8-master:
  Fix Win64 compile warnings
  p8-platform-config: fix lookup paths
@srg70
Copy link
Contributor Author

srg70 commented Nov 4, 2018

Sure, I created another one #39.
Thanks for review.

@srg70 srg70 closed this Nov 4, 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
Development

Successfully merging this pull request may close these issues.

4 participants