?? threadedclient.cpp
字號:
//author:chenwanqiao
/*
ThreadedClient.cpp
A threaded database client.
*/
#include <iostream>
#include "servers.h"
#ifdef _MSC_VER
#include <windows.h>
#include <winbase.h>
#include <process.h>
Personal *pers =NULL;
AccountInfo *acct=NULL;
typedef LONGLONG time_ms_t;
void PersonThread(void* args)
{
pers = GetPersonalInformation( *((int *)args) );
}
void AccountThread(void* args)
{
acct = GetAccountInformation(*((int *)args) );
}
time_ms_t getTimeInMilliseconds() {
SYSTEMTIME stime;
GetSystemTime( &stime );
FILETIME ftime;
LARGE_INTEGER time;
SystemTimeToFileTime( &stime, &ftime ); /* if this fails... */
time.HighPart = ftime.dwHighDateTime;
time.LowPart = ftime.dwLowDateTime;
/* FileTime is in 100ns intervals since 1/1/1601 */
return time.QuadPart / 10000;
}
#endif
/*
ostream &operator <<( ostream &out, string *str ) {
Send a string to an output stream.
*/
ostream &operator<<(ostream &out, string *str) {
if (str)
return out << str->data();
else
return out;
}
/*
int main( int argc, char *argv[]
You should modify this function to use threads.
*/
int main( int argc, char *argv[] ) {
if (argc != 2) {
cerr << "usage: " << argv[0] << " [account_number]" << endl;
exit(1);
}
int account = atoi( argv[1] );
time_ms_t start = getTimeInMilliseconds();
cout << "Retrieving...";
cout.flush();
HANDLE thread1;
HANDLE thread2;
thread1 = (HANDLE) _beginthread( PersonThread, 0, &account );
thread2 = (HANDLE) _beginthread( AccountThread, 0, &account );
HANDLE threadGroup[2];
if (thread1 == (HANDLE) -1L||thread2 == (HANDLE) -1L) {
cerr << "Couldn't create two threads!" << endl;
exit(1);
}else {
threadGroup[0]=thread1;
threadGroup[1]=thread2;
WaitForMultipleObjects( 2,threadGroup, true, INFINITE );
}
time_ms_t end = getTimeInMilliseconds();
ULONGLONG elapsed = end - start;
cout << "done (" << elapsed << " ms)" << endl;
if (!pers || !acct)
cout << "No client matches that account number" << endl;
else {
cout << account << ": " << pers->FirstName << " "
<< pers->LastName << endl;
cout << pers->Address << endl;
cout << "Balance: " << acct->Balance << ", "
<< acct->Pending
<< " pending, " << acct->Share << " share" << endl;
}
if (pers) delete pers;
if (acct) delete acct;
return 0;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -