Salve,
volevo sapere come fare operazioni sul timestamp per vedere quando tempo dista tra due momenti, ad esempio da quando è stato lanciato un thread ad ora. Mi piacerebbe che ritornasse una stringa tipo: hh:mm:ss che identificasse l'uptime.
Mettiamo che ho una struttura del genere:
che uso in una mappa del genere:codice:enum { MAXCHAR = 128; }; struct user { char szUserAgent[MAXCHAR]; char timestamp // inventato }
Lancio un nuovo thread:codice:// Global std::map<int, user> row; CRITICAL_SECTION row_mutex; // avrò poi un'altro thread // che legge la mappa ogni sec.
Ora mi piacerebbe che nel thread dedicato alla lettura mi visualizzasse non solo i due campio ma anche un terzo campo creato sul momento che altro non è che la differenza tra now() e user.timestamp:codice:{ int threadid = (int)GetCurrentThreadId(); user u = { _T("marameo/1.0"), now() }; // now() inventato EnterCriticalSection(&row_mutex); row.insert( std::make_pair(threadid, u) ); LeaveCriticalSection(&row_mutex); }
Ora certe cose le ho messe solo per rendere l'idea..ma si può fare cmq?codice:{ while(1) { if(stopThreadFlag) break; std::map<int, user>::iterator itt; EnterCriticalSection(&row_mutex); for(itt=row.begin(); itt!=row.end(); ++itt) { std::cout << " User: " << itt.second.szUserAgent; std::cout << " Timestamp: " << itt.second.timestamp; std::cout << " Up Time: " << ( now() - itt.second.timestamp ); } LeaveCriticalSection(&row_mutex); Sleep(1000); } }
grazie