📄 threadedclient.cpp
字号:
/*
ThreadedClient.cpp
A threaded database client.
*/
#include <iostream>
#include "servers.h"
#ifdef _MSC_VER
#include <windows.h>
#include <winbase.h>
#include <process.h>
typedef __int64 INT64_T;
typedef unsigned __int64 UINT64_T;
typedef LONGLONG time_ms_t;
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;
}
/*
ostream & operator<< ( ostream &out, INT64_T num )
Print a 64-bit unsigned integer to the given output stream.
*/
ostream &operator<<( ostream &out, INT64_T snum ) {
#define _OSTR_LL_LEN 21
if (snum) {
char buffer[_OSTR_LL_LEN];
int i = _OSTR_LL_LEN - 1;
UINT64_T num;
if (snum < 0) num = -snum; else num = snum;
buffer[i] = '\0';
while (num) {
buffer[--i] = ('0' + (int) (num % 10));
num /= 10;
}
if (snum < 0) buffer[--i] = '-';
return out << buffer + i;
}else return out << '0';
}
/*
These are the two thread routines, each of which will run as
as a separate thread
*/
Personal *global_personal = NULL;
void __cdecl personal_thread( void *arg ) {
int acct = *( (int *) arg );
global_personal = GetPersonalInformation( acct );
}
AccountInfo *global_account = NULL;
void __cdecl account_thread( void *arg ) {
int acct = *( (int *) arg );
global_account = GetAccountInformation( acct );
}
/*
int main( int argc, char *argv[]
You should modify this function to use threads.
*/
int main( int argc, char *argv[] ) {
HANDLE handles[2];
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();
handles[0] = (HANDLE) _beginthread( personal_thread, 0, &account );
if (handles[0] == (HANDLE) -1L) {
cout << "Couldn't create thread!" << endl;
exit(1);
}
handles[1] = (HANDLE) _beginthread( account_thread, 0, &account );
if (handles[1] == (HANDLE) -1L) {
cout << "Couldn't create thread!" << endl;
exit(1);
}
WaitForMultipleObjects( 2, handles, TRUE, INFINITE );
cout << "done" << endl;
time_ms_t end = getTimeInMilliseconds();
cout << "done (" << end - start << "ms)" << endl;
if (global_personal) {
cout << account << ": " << global_personal->FirstName << " "
<< global_personal->LastName << endl;
cout << global_personal->Address << endl;
cout << "Balance: " << global_account->Balance << ", "
<< global_account->Pending
<< " pending, " << global_account->Share << " share" << endl;
}
delete global_personal;
delete global_account;
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -