⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 threadedclient.cpp

📁 卡耐基SSD6全部选择题和练习题解决方法。
💻 CPP
字号:
/*
	ThreadedClient.cpp

	A threaded database client.
 */

#include <iostream>

#include "servers.h"

#ifdef _MSC_VER
#include <windows.h>
#include <winbase.h>
#include <process.h>
extern "C" struct AccountInfo;
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;
} 

typedef struct ThreadArgs{
	int count;
	void* returnValue;
}ThreadArgs;


void __cdecl threadfunc1( void *args ) {
    ThreadArgs *myArg = (ThreadArgs *) args;
	myArg->returnValue = GetPersonalInformation( myArg->count );
}

void __cdecl threadfunc2( void *args ) {
	ThreadArgs *myArg = (ThreadArgs *) args;
	myArg->returnValue = GetAccountInformation( myArg->count );
}


/*
	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();
  

	ThreadArgs* myargs1 = (ThreadArgs*)malloc(sizeof(ThreadArgs));
	ThreadArgs* myargs2 = (ThreadArgs*)malloc(sizeof(ThreadArgs));
	myargs1->count = account;
	myargs2->count = account;

	HANDLE thread1;
	HANDLE thread2;

    thread1 = (HANDLE) _beginthread( threadfunc1, 0, myargs1 );
	thread2 = (HANDLE) _beginthread( threadfunc2, 0, myargs2 );
    
	Personal *pers = (Personal*)(myargs1->returnValue);
	AccountInfo *acct = (AccountInfo*)(myargs2->returnValue);


	HANDLE handles[2];
	handles[0] = thread1;
	handles[1] = thread2;

	WaitForMultipleObjects(2, handles, 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 + -