Hello,
I'm someone that's always looking for the perfect design for an application. I can't stand it when someone is creating ugly code so I'm always using coding guidelines for my projects. One of the rules is: Don't use global variables unless there isn't any other way.
With this in mind; hear my problem ;)
I want to create a multithreaded game in C++. Some code:
#include "stdafx.h"
#include "Thread1.h"
#include "Thread2.h"
int main(int argc, char *argv[])
{
Thread1 *Thrd1 = new Thread1(); // This could be our render thread...
Thread2 *Thrd2 = new Thread2(); // And this could be our drink some beer thread...
Thrd1->Start();
Thrd2->Start();
delete Thrd1;
delete Thrd2;
return 0;
}
Just a little application which is starting two threads and will execute them. Now these threads both have a run method where they're printing text to the screen (for example). This of course, needs be synchronized.
// Thread1
while (IsRunning)
{
Lock->Acquire();
std::cout << "Written from thread 1" << std::endl;
Lock->Release();
}
// Thread2
while (IsRunning)
{
Lock->Acquire();
std::cout << "Written from thread 2" << std::endl;
Lock->Release();
}
As you see is the lock used in both threads... hey this is in conflict with my coding guidelines ;) So my question: How can I use multithreading in an elegant way without the use of global variables.