cout Problem



  • class TimerManager{
      public:
        TimerManager();
        ~TimerManager();    // in milli secs                                 // lP ist Optional!
        void AddTimer ( int TimeToWait, void ( *Function ) ( PVOID lParam ), PVOID lParam = NULL );
      private:
    
        struct Timer{
          PVOID lParam; 
          double TimeToWait;
          void (*Function) ( PVOID lParam );
          double StartTime;
          explicit Timer( int TTW, void (*F)( PVOID ), PVOID lP ):TimeToWait(((double)TTW)/1000),Function(F),lParam(lP),StartTime(0){}
        };
    
        struct FuncThreads{
          HANDLE hThread;
          bool * bRuns;
          bool Busy;
          PVOID lParam;
          void (*Function) ( PVOID lParam );
          explicit FuncThreads ( HANDLE, void (*F)( PVOID ), PVOID lP, bool * R ):Busy(false),bRuns(R),Function(F),lParam(lP){}
        };
    
        std::vector < Timer * >Timers;
        std::vector < FuncThreads * > hThreads;
        HANDLE hThread;
        bool Runs;
    
        static DWORD WINAPI TimerThread ( PVOID lParam );
        static DWORD WINAPI FuncThread ( PVOID lParam );
    };
    
    TimerManager::TimerManager():Runs(true),hThread(NULL){
      DWORD DUMP;
      hThread = CreateThread( NULL, NULL, TimerThread, (PVOID)this, NULL, &DUMP );
      // Create Idle threads.
      for ( int i = 0; i < 4; i++ ){
        hThreads.push_back ( new FuncThreads( CreateThread( NULL, NULL, FuncThread, (PVOID)NULL, CREATE_SUSPENDED, &DUMP ),
                                              NULL,
                                              NULL,
                                              &Runs ) );
      }
    }
    
    TimerManager::~TimerManager(){
      Runs = false;
      if ( hThread != NULL ){
        DWORD ExitCode;
        GetExitCodeThread(hThread,&ExitCode);
        TerminateThread(hThread,ExitCode);
      }
      for ( std::vector < FuncThreads * >::iterator It = hThreads.begin(); It < hThreads.end(); It++ ){
        DWORD ExitCode;
        GetExitCodeThread((*It)->hThread,&ExitCode);
        TerminateThread((*It)->hThread,ExitCode);
        delete *It;
      }
      for ( std::vector < Timer * >::iterator It = Timers.begin(); It < Timers.end(); It++ )
        delete *It;
    
    }
    void TimerManager::AddTimer ( int TimeToWait, void ( *Function ) ( PVOID ), PVOID lParam ){
      Timers.push_back(new Timer(TimeToWait,Function,lParam));
    }
    
    DWORD WINAPI TimerManager::TimerThread ( PVOID lParam ){
      TimerManager * Object = reinterpret_cast < TimerManager* >(lParam);
      __int64 Frequency, CurrentCount;
      double CurrPos;
      if (!QueryPerformanceFrequency((LARGE_INTEGER*)&Frequency))
        return 0;
      while ( Object->Runs ){
        QueryPerformanceCounter((LARGE_INTEGER*)&CurrentCount);
        CurrPos = CurrentCount / (double)Frequency;
        for ( std::vector < Timer * >::iterator tIt = Object->Timers.begin(); tIt < Object->Timers.end(); tIt++ ){
          if ( !(*tIt)->StartTime ){
            (*tIt)->StartTime = CurrPos;
            continue;
          }
           if ( (( CurrPos - (*tIt)->StartTime )) >= (*tIt)->TimeToWait ){
            if ( (*tIt)->Function ){
              bool Found = false;
              for ( std::vector < FuncThreads * >::iterator fIt = Object->hThreads.begin(); fIt < Object->hThreads.end(); fIt++ ){
                if ( (*fIt)->Busy )continue;
                (*fIt)->Function = (*tIt)->Function;
                (*fIt)->lParam = (*tIt)->lParam;
                (*fIt)->Busy = true;
                ResumeThread((*fIt)->hThread);
                Found = true;
              }
              if ( !Found ){
                DWORD DUMP;
                // Arbeite wenn busy...
                FuncThreads * ftTemp = new FuncThreads(NULL,(*tIt)->Function,(*tIt)->lParam,&(Object->Runs));
                HANDLE hTemp = CreateThread( NULL, NULL, FuncThread, (PVOID)ftTemp, NULL, &DUMP );
                ftTemp->hThread = hTemp;
                ftTemp->Busy = true;
                Object->hThreads.push_back(ftTemp);
                ResumeThread(hTemp);
              }
    
              //(*It)->Function((*It)->lParam);
            }
            (*tIt)->StartTime = 0;
          }
        }
      }
      return 0;
    }
    
    DWORD WINAPI TimerManager::FuncThread ( PVOID lParam ){
      FuncThreads * Object = reinterpret_cast < FuncThreads * >(lParam);
      while ( *(Object->bRuns) ){
        if ( Object->Busy ){
          Object->Function( Object->lParam );
          Object->Function = NULL;
          Object->lParam = NULL;
          Object->Busy = false;
          SuspendThread(GetCurrentThread());
        }
      }
      return 0;
    }
    

    ausprobier anwendung xD

    int M = 0,S = 0, MS = 0;
    
    void Increase( PVOID lParam  ){
     MS += 100;
     if ( MS >= 1000 ){
      MS = 0;
      if ( ++S >= 60 ){
        S = 0;
        M++;
      }
     }
    }
    
    void Output( PVOID lParam ){
    
      //cout << "Time: M,S,MS " << M << " : " << S << " : " << MS << endl;
      return;
    }
    
    int main(int argc, char* argv[])
    {
    
      TimerManager * Man = new TimerManager();
    
      Man->AddTimer(100,Increase);
      Man->AddTimer(3000,Output);
    
      //Man->AddTimer(3000,MeineKlasse->TimerProcc,(PVOID)MeineKlasse);
    
      Sleep(1212000);
      delete Man;
      return 0;
    }
    //---------------------------------------------------------------------------
    

    Hi,

    also habe gerade das problem das wenn ich den timer so schreibe wie er im moment ist, er warsch. durch den aufruf aus anderen threads probleme mit der konsolen ausgabe kriegt, kann ich dies durch eine explizite zuweisung des konsolen handles zu cout wieder beheben, und wenn, wie gehts sowas :D?

    edit --

    hat sich erledigt, der fehler lag in zeile 90, break vergessen .


Anmelden zum Antworten