Concurrent C++ Concurrent Programming With Class.docx

上传人:b****8 文档编号:9236158 上传时间:2023-02-03 格式:DOCX 页数:62 大小:39.78KB
下载 相关 举报
Concurrent C++ Concurrent Programming With Class.docx_第1页
第1页 / 共62页
Concurrent C++ Concurrent Programming With Class.docx_第2页
第2页 / 共62页
Concurrent C++ Concurrent Programming With Class.docx_第3页
第3页 / 共62页
Concurrent C++ Concurrent Programming With Class.docx_第4页
第4页 / 共62页
Concurrent C++ Concurrent Programming With Class.docx_第5页
第5页 / 共62页
点击查看更多>>
下载资源
资源描述

Concurrent C++ Concurrent Programming With Class.docx

《Concurrent C++ Concurrent Programming With Class.docx》由会员分享,可在线阅读,更多相关《Concurrent C++ Concurrent Programming With Class.docx(62页珍藏版)》请在冰豆网上搜索。

Concurrent C++ Concurrent Programming With Class.docx

ConcurrentC++ConcurrentProgrammingWithClass

 

ConcurrentC++:

ConcurrentProgrammingWithClass(es)

N.H.Gehani

W.D.Roome

AT&TBellLaboratories

MurrayHill,NewJersey07974

1.Introduction

Recentlytwoupward-compatibleextensionsoftheCprogramminglanguage[Kernighan1978]havebeen

developed:

ConcurrentC[Gehani&Roome1986;Cmelik,Gehani&Roome1987]whichprovidesparallel

programmingfacilities,andC++[Stroustrup1986],whichprovidesdataabstractionfacilities.C++does

notprovideparallelprogrammingfacilities,andConcurrentCdoesnotprovidedataabstractionfacilities.

Althoughdataabstractionfacilitiesareimportantforwritingconcurrentprograms,wedidnotprovidethem

inConcurrentCbecausewedidnotwanttoduplicatetheC++researcheffort.Instead,wedecidedthatwe

wouldeventuallyintegrateC++andConcurrentCfacilitiestoproducealanguagewithbothdata

abstractionandparallelprogrammingfacilities:

ConcurrentC++.Thiswasapragmaticdecision.Wefelt

thatitwouldbebetterifConcurrentC++hadthesamedataabstractionasC++becausethiswouldmake

ConcurrentC++upwardcompatiblewithC++andConcurrentC++wouldthenbemoreattractiveforC++

usersinterestedinwritingconcurrentprograms.

Havinggainedexperienceinbothlanguages,wedecidedtomergeC++andConcurrentCtoproduce

ConcurrentC++.Dataabstractionandparallelprogrammingfacilitiesareorthogonal.Despitethis,the

mergerofConcurrentCandC++raisedseveralintegrationissues.Inthispaper,wewillgivebrief

introductionstoC++andConcurrentCandthenpresenttwoexamplesofhowclasses,thedataabstraction

facilityofC++,canbevaluableforwritingconcurrentprograms.Afterthiswewilldiscusstheissuesin

integratingC++andConcurrentCandthendescribethecurrentstateofaffairs.

2.BriefSummaryofC++

TheC++dataabstractionfacilityiscalledtheclass.Classdeclarationsconsistoftwoparts:

aspecification

andabody.Theclassspecificationrepresentstheclass‘‘userinterface’’.Itcontainsalltheinformation

necessaryfortheuserofaclass.Theclassspecificationalsocontainsinformationnecessaryforthe

compilertoallocateclassobjects.Theclassbodyconsistsofbodiesoffunctionsdeclaredintheclass

specificationbutwhosebodieswerenotgiventhere.Intherestofthissection,wewillgiveabrief

overviewoftheC++classfacility.Foradetailedexplanation,seeTheC++ProgrammingLanguage

[Stroustrup1986].

Classspecificationshavetheform

classname{

privatecomponents



publiccomponents

};

Theprivatecomponentsofaclassaredataitemsandfunctionsthatimplementclassobjects.These

representinternaldetailsoftheclassandcannotbeaccessedbytheuserofaclass.

Thepublicclasscomponentscanbedataitems,constructors,destructors,memberfunctions(operators),

andfriendfunctions(operators).Thepubliccomponents(tobeprecise)representtheclassuserinterface.

Thesearethecomponentsthattheuserofaclasscanuseorcall.Constructorsarefunctionsthatarecalled

automaticallytoconstructaclassvalue.Destructorsarealsofunctionsthatarecalledautomaticallywhen

thescopeofaclassobjectisleft;theyareintendedforperforming‘‘cleanup’’chores.Memberandfriend

functionsarefunctionsthatareusedtomanipulateclassobjects.1Notethatoperatorscanbe‘‘overloaded’’

⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽

public:



⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽

 

-2-

 

inC++.

Asanexampleofclasses,considertheclasscomplex:

⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽

⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽

classcomplex{

doublere,im;

complex(doubler,doublei);

complex(doubler);

complex();

doublereal();

doubleimag();

friendcomplexoperator+(complex,complex);

friendcomplexoperator-(complex,complex);

friendcomplexoperator-(complex);

friendcomplexoperator*(complex,complex);

friendcomplexoperator/(complex,complex);

friendintoperator==(complex,complex);

friendintoperator!

=(complex,complex);

};

Thefirstthreelinesinthepublicpartofthecomplexclassshownabovearedeclarationsofconstructors.

Anappropriateconstructor,selectedaccordingtotheinitializationvaluessupplied,isautomaticallycalled

whencomplexvariablesaredefined.Thenextlinedeclaresthememberfunctionsrealandimag,and

theremaininglinesdeclarefriendoperators.

Wewillshowthedefinitionsofoneconstructorfunction,onememberfunctionandonefriendoperator.

#include"complex.h"

complex:

:

complex(doubler,doublei)

{



re=r;im=i;

}



doublecomplex:

:

real()

{

returnre;

}



complexoperator+(complexa,complexb)

{

returncomplex(a.re+b.re,a.im+b.im);

}

Herearesomeexamplesillustratingusesofthecomplexclass:

 

__________________

1.Inthispaper,weshallnotdistinguishbetweenmemberandfriendfunctionsexcepttosaythattheirsyntaxisslightlydifferent.

public:



⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽

⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽





⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽

File:

complex.h

 

-3-

 

⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽

complexa=complex(5.0,6.0),b=complex(1.0,1.0);

c=a+b;

Notethatforillustrationpurposes,variablesaandbhavebeeninitializedexplicitlyandvariablechasnot

beenexplicitlyinitialized.Forvariablesaandb,thefirstconstructorshownisautomaticallyinvokedto

constructtheinitialvalue,andforvariablecthethirdconstructorisinvokedtoconstructthedefaultinitial

value.Theconstructorinvokeddependsupontheinitialargumentssupplied(ornotsupplied)when

definingaclassvariable.

3.BriefSummaryofConcurrentC

AConcurrentCprogramconsistsofasetofcomponents,calledprocesses,thatexecuteinparallel.

ConcurrentCprovidesfacilitiesfordeclaringandcreatingprocesses,processsynchronizationand

interaction,processterminationandabortion,priorityspecificationandwaitingformultipleevents,

amongstotherthings.

ConcurrentCprocessesinteractwitheachotherbymeansoftransactions.Transactionsareassociatedwith

processesandthesemaybethoughtofasservicesthatarecalled(used)byotherprocesses.Processes

callingservicesmaybethoughtofas‘‘client’’processesandprocessesoffering(andaccepting)services

maybethoughtofas‘‘server’’processes.Weshallusetheterms‘‘calling’’and‘‘client’’,andtheterms

‘‘called’’and‘‘server’’processesinterchangeably.Therearetwokindsoftransactions:

synchronous(or

blocking)andasynchronous(ornonblocking).Thecallingprocesssendsinformationtotheserverprocess

bymeansoftransactionparameters,andincaseofsynchronoustransactioncalls,thecalledprocesscan

returninformationtothecallingprocessasthetransactionresult.

Aprocesscallingasynchronoustransactionisblockeduntilthecalledprocessacceptsthetransaction

(givestherequestedservice),executesthecodeassociatedwiththetransaction,andreturnsthetransaction

result,ifany.Synchronoustransactionsallowbidirectionalcommunicationbetweentheinteracting

processes.Synchronoustransactioncallscanalsobeusedforprocesssynchronization:

thecallingand

receivingprocesssynchronizeatthetransactioncall.Theyarefreetoresumeindependentoperationafter

thetransactioncallhasbeenaccepted.Infact,transactioncallscanbeusedjustforsynchronization(a

transactionneednothaveanyparametersanditcanhavethevoidresulttype).

Aprocesscallinganasynchronoustransactionisallowedtocontinueimmediatelyaftermakingthecall.

Thetransactionisacceptedbythecalledpro

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 考试认证 > 财会金融考试

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1