I have a multithreaded application. However I noticed, I’m using OpenGL and this should not work well without semaphores. And it did. I added semaphores later, because they should be used.
Not so great is the fact that I now have a multithreaded application which does not really seem to utilise multiple threads or cores on the cpu.
My load balance always seems to be towards a single cores (of 4) with a difference of atleast 40%.
Do I need to switch a trigger in Qt’s project file, or am I overlooking something else? It could be that this behaviour is expected, but clarification would be nice if so.
My platform is Ubuntu 11.10.
class DrawChunkThread : public QThread
{
public:
DrawChunkThread(World *world, int x, int z);
void run();
World *world;
int x;
int z;
static QSemaphore *sem;
private:
};
QSemaphore *DrawChunkThread::sem = new QSemaphore(1);
DrawChunkThread::DrawChunkThread(World *world, int x, int z) {
this->world = world;
this->x = x;
this->z = z;
}
void DrawChunkThread::run()
{
world->drawChunk(x, z);
if (world->isInside) {
world->drawInsideChunk(x, z);
}
}
If the semaphores you added create a critical section that is either too long or too contended (or both), you will have scalability problems, because not all threads are allowed to execute the code at the same time and waiting on a semaphore triggers a context-switch, which adds further overheads.
In general, your parallel application containing a serial portion cannot scale more than the parallel portion divided by the number of cores, as per Amdahl’s law.
Without any concrete code, I cannot think of any other advice to give you.