Alex Edwards 321 Posting Shark

I'm getting a really odd error in my program.

The lvl value retains its initial value if it's left alone, but when the program runs after you've assigned it a value it get's a ridiculous number... then the previous number after another assignment... and the process continues.

Here's the code.

#include <cstdlib>
#include <iostream>
#include <windows.h>

using namespace std;

/**

Self-Learning session: Multi-threading in C++

Lesson learned: I might need to take a look into synchronization in order
to give proper values to objects that are constantly beind monitiored by other
Threads.

Not sure why lvl is always the previous number to whichever I assign it
from std::cin

*/

class Menu;
class Stats;
DWORD WINAPI runStats(VOID*);
DWORD WINAPI runMenu(VOID*);

class Stats{
      public:
             int hp, mp, lvl;
             bool running;
             
             Stats() : running(false) {hp = 0; mp = 0; lvl = 0;};
             
             void run(){
                if(!running){  
                  running = true;          
                            
                  while(true){
                       Sleep(5000);
                       cout << "Current HP is... " << hp <<"\n" << endl;
                       Sleep(2000);
                       cout << "Current MP is... " << mp <<"\n" << endl;
                       Sleep(2000);
                       cout << "Current LVL is... " << lvl <<"\n" << endl;
                       Sleep(2000);
                  }
                  running = false; //this should not occur since the above is an infinite loop.
                }
             }
};

class Menu{             
      public:
             
             Stats *myStats;
             bool running;
             
             Menu(Stats *objStats) : running(false){
                    myStats = objStats;    
             }
             
             void run(){
                  if(!running){
                      running = true;        
                      bool stopped = false;
                 
                          do{
                              char answer1[200] = {0};
                      
                              cout << "Do you want to alter the stats?" << endl;
                              cin >> answer1;
                      
                              if(answer1[0] == 'y' || answer1[0] == 'Y'){
                                    int newHP, newMP, newLvl;          
                                    cout << "New HP value?: " << endl;
                                    cin >> newHP;
                                    myStats->hp = newHP;
                                    cout << "New MP value?: " << endl;
                                    cin >> newMP;
                                    myStats->mp = newMP;
                                    cout << "New Lvl value?: " << endl;
                                    myStats->lvl = newLvl;
                                    cin >> newLvl;       
                              }
                              else{
                                    char answer2[200] = {0};
                                    cout << "Do you want to quit?" << endl;
                                    cin >> answer2;
                                    
                                    if(answer2[0] == 'y' || answer2[0] == 'Y')
                                           stopped = true;
                              }
                          }while(!stopped);
                      running = false;
                  }
             }        
};

Stats *theStats;
Menu *theMenu;

int main(VOID){
    
    const unsigned THREAD_AMOUNT = 2;
    
    //Note: Using similar syntax from website--
    //http://msdn.microsoft.com/en-us/library/ms682516(VS.85).aspx
    //--to follow with the tutorial on using threads.
    
    //Need to create handles that threads are assosciate with
    HANDLE myThreads[THREAD_AMOUNT];
    
    //create DWORDS for the thread's ID??
    DWORD threadID[THREAD_AMOUNT];
    
    //It might be best to create pointers to objects the threads will use,
    //so they can be cast to void pointers
    theStats = new Stats;
    theMenu = new Menu(theStats);
                                 //
                 myThreads[0] = CreateThread( 
            NULL,                   // default security attributes
            0,                      // use default stack size  
            runMenu,       // thread function name
            (VOID*)NULL,          // argument to thread function 
            0,                      // use default creation flags 
            &threadID[0]); //Thread ID number returend from this thread function
    
                                 //
                 myThreads[1] = CreateThread( 
            NULL,                   // default security attributes
            0,                      // use default stack size  
            runStats,       // thread function name
            (VOID*)NULL,          // argument to thread function 
            0,                      // use default creation flags 
            &threadID[1]); //Thread ID number returend from this thread function  
    
    //A type of "joiner" for the threads
    WaitForMultipleObjects(THREAD_AMOUNT, myThreads, TRUE, INFINITE);
    
 //   cin.get(); //dont need, since program is waiting for the Threads to end
    return 0;
}

DWORD WINAPI runStats(VOID* arg){theStats->run();};
DWORD WINAPI runMenu(VOID* arg){theMenu->run();};

Note: The comments were included because I was teaching myself some of the concepts of threading via experience. They can be ignored.

Edit: Whoops... really sorry. I think I found the error......

Can this topic be erased? =/

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.