Untitled
unknown
plain_text
a year ago
1.6 kB
9
Indexable
// Worker function
/**
* @brief Worker function for the thread pool.
*
* This function is executed by each worker thread in the thread pool. It sets the thread priority,
* then enters a loop where it waits for tasks to be available in the task queue. When a task is
* available, it retrieves and executes the task. The loop continues until the thread pool is
* stopped and there are no more tasks left in the queue.
*
* @note This function is intended to be run in a separate thread.
*/
void ThreadPool::Worker() {
// Set thread priority when the worker starts
SetThreadPriority(pthread_self(), thread_priority_);
while (true) {
Task task;
{
std::unique_lock<std::mutex> lock(queue_mutex_);
condition_.wait(lock, [this] { return stop_ || !tasks_.empty(); });
if (stop_ && tasks_.empty()) {
return;
}
task = tasks_.top();
tasks_.pop();
}
task.func(); // Execute the task
}
}
// Set thread priority depending on the platform
/**
* @brief Sets the priority of a thread.
*
* This function sets the scheduling priority of a thread using its native handle.
* It uses the POSIX pthread_setschedparam function to set the priority.
*
* @param handle The native handle of the thread whose priority is to be set.
* @param priority The priority value to be set for the thread.
*/
void ThreadPool::SetThreadPriority(std::thread::native_handle_type handle, int priority) {
// Linux priority (using pthread)
struct sched_param param;
param.sched_priority = priority;
pthread_setschedparam(pthread_self(), SCHED_FIFO, ¶m);
}Editor is loading...
Leave a Comment