Do I need to multi-thread?

closed account (2NywAqkS)
I'm making a simple console based nuclear reactor sim game. The main loop looks like this:
1
2
3
4
5
6
7
8
9
10
11
12
bool done = false;
while(!done)
{
	char cmd;
	cout << "CMD>";
	cin >> cmd;
	switch (cmd)
	{
		// Run commands
	}
	Calculate();
}

The problem with this is that I want to be calculating the reactor in real time and obviously this main loop pauses with the use of 'cin << cmd'. Now from what I know, the solution to this would be multi-threading. One thread running the input commands and the other running the calculate functiuon.

Is multi-threading the solution to this problem?
How would I go about doing this? (preferably without WIN32 API)
And, would this require a dualcore proccesor?
closed account (S6k9GNh0)
There are several solutions.

1. Figure out how to poll for input. In this case, the curses API should probably be able to help you.

2. Throw the calculation off to another thread. In this case, it would be a performance gain (albeit the slightest) as well. Unfortunately, it would greatly increase the complication of the program. std::thread should be able to help you here.

3. Who knows?
Last edited on
closed account (zb0S216C)
Rowan836 wrote:
"I'm making a simple console based nuclear reactor sim game."

I was thinking of creating the exact same thing a while back. Strange. Any road...

Based on what you've said, the main thread is waiting for input. As you know, this would suspend the entire program until something is given as input. Of course, this is not ideal since computations such as reactor temperatures, water pressure, etcetera need to be continuously updated.

The resolution to this would indeed be threading. I have something like this in mind:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
struct Reactor_Information
{
  public:
    Reactor_Information( )
      : // Initialise all data-members
    { }

  private:
    Mutex Lock_;

    int DEG_Vessel_Temp_;
    int PSI_Water_Pressure_;
    int DEG_Water_Temp_;
    // ...and so on...

  public:
    static void Update( void *Args_ );
};

void Reactor_Information::Update( void *Args_ )
{
  Reactor_Information *Info_( static_cast< Reactor_Information * >( Args_ ) );
  Info_->Lock_.Lock( );
    // Update the information...
  Info_->Lock_.Unlock( );
}

int main( )
{
  //...
}

Which ever thread calls "Reactor_Information::Update( )" will attain the lock which in turn will allow the thread to update the reactor's information -- this allows the main thread to wait for input while the reactor information is being periodically updated.

[Note -- The declaration of "Reactor_Information::Update( )" may vary depending on the library you choose -- end note]

Threading libraries come is different forms:

-- The OS's API
-- Boost
-- C++11's Standard Library

Rowan836 wrote:
"And, would this require a dualcore proccesor?"

No; the processor will use context switching as they all do.

Wazzak
Last edited on
closed account (2NywAqkS)
thanks :)
Topic archived. No new replies allowed.