我有一個正在繪製gtkmm窗口的實時動畫的程序。我有我的主線程gui線程和一個工作程序threa,使用cairo將動畫幀渲染到圖像表面。工作線程每33mS由主線程發出信號。現在,我有我的應用程序創建一個新的渲染線程在每個超時幀必須呈現時。我怎麼能創建一種線程池,在超線程的gui線程中,我的工作線程會喚醒並呈現一個幀,該幀表示幀已完成的GUI線程,然後返回到睡眠狀態並等待再次發送信號。信號喚醒線程
Q
信號喚醒線程
0
A
回答
1
我不是渲染和開羅的專家,但是線程一般可以互相發信號。更簡單的方法是使用條件變量或互斥體。您的所有工作線程都可以等待互斥鎖,並且GUI線程可以在超時時釋放互斥鎖,並且GUI線程本身可以在另一個線程上等待。工作線程獲取互斥量可以渲染幀和信號GUI線程,然後可以返回第一個互斥量等待來自GUI線程的下一個信號。
0
您可以使用master-worker
方法,其中主線程爲master
,它處理要渲染的幀,並且workers
從工作線程的pool
中提取。
有一個公共域thread pool
實現可從DevGuy,您可以使用。
我覺得這樣的事情應該工作:
#include <dg/dg.h>
#include <dg/thread/threadpool.h>
#include <dg/thread/impl.h>
#include <dg/impl.h>
#include <boost/bind.hpp>
// a mutex is needed if the workers touch shared data
boost::mutex mutex;
struct work
{
void operator()()
{
boost::mutex::scoped_lock lock(mutex);
// this is where a worker does its thing
}
worker(/* your constructor params */)
{
// ...
}
worker(const worker &w)
{
// ...
}
// internal work data
};
int main(int argc, char* argv[])
{
// a pool with 5 threads that ca queue up to 100 jobs
dg::thread::ThreadPool pool(5,100);
for (;;)
{
// create a piece of work
work w;
// execute the work
pool.invoke(w);
// some exit condition
}
// wait for unfinished work
pool.wait();
}
這裏是另外一個使用example。
是的,我認爲跨線程信號將是最好的方法。也許Glib :: Dispatcher是我需要做的工作 – Talguy 2010-06-18 12:28:43