tasks.h 1.1 KB

1234567891011121314151617181920212223242526
  1. #ifndef __TASKS_H
  2. #define __TASKS_H
  3. #include "task.h"
  4. #include <pthread.h>
  5. #include "concurrent_queue.h"
  6. // This class is a queue of tasks.
  7. // add_task and do_tasks can be called from separate threads.
  8. // add_task will be called from the main thread.
  9. // do_tasks will be called from the background thread.
  10. // All do_tasks does is process each task in chatroom_tasks one by one by calling the do_task() method of the task object.
  11. // wait_for_tasks will be called from the background thread.
  12. class tasks
  13. {
  14. public:
  15. tasks();
  16. ~tasks();
  17. void add_task(task *task); // Adds a task to the list of tasks to do. Calling add_task might signal other threads that a task is ready to process.
  18. void do_tasks(); // Does all tasks. Returns when no tasks are waiting.
  19. void wait_for_tasks(int delay_in_seconds); // Wait until there are tasks to do or delay_in_seconds, whichever comes first. If delay_in_seconds = 0, then wait forever.
  20. private:
  21. concurrent_queue<task *>*chatroom_tasks; // List of tasks.
  22. pthread_mutex_t queue_mutex; // This mutex should be locked if no tasks are waiting.
  23. };
  24. #endif