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

📄 threadedclient.cpp

📁 SSD6卡耐基梅陇大学联系6答案 绝对正确 SSD6数据结构 是一门很重要的课程 希望对大家有帮助
💻 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 + -