async.h 725 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #ifndef BB_STD_ASYNC_H
  2. #define BB_STD_ASYNC_H
  3. #include <thread>
  4. #include <atomic>
  5. #include <mutex>
  6. #include <condition_variable>
  7. namespace bbAsync{
  8. struct Semaphore{
  9. int count;
  10. std::mutex mutex;
  11. std::condition_variable cond_var;
  12. Semaphore():count( 0 ){
  13. }
  14. Semaphore( int count ):count( count ){
  15. }
  16. void wait(){
  17. std::unique_lock<std::mutex> lock( mutex );
  18. while( !count ) cond_var.wait( lock );
  19. --count;
  20. }
  21. void signal(){
  22. std::unique_lock<std::mutex> lock( mutex );
  23. ++count;
  24. cond_var.notify_one();
  25. }
  26. };
  27. struct Event{
  28. void post();
  29. void post( double delay );
  30. virtual void dispatch()=0;
  31. };
  32. void setPostEventFilter( void(*filter)(Event*) );
  33. }
  34. #endif