Concurrent C++ Concurrent Programming With ClassWord下载.docx
《Concurrent C++ Concurrent Programming With ClassWord下载.docx》由会员分享,可在线阅读,更多相关《Concurrent C++ Concurrent Programming With ClassWord下载.docx(62页珍藏版)》请在冰豆网上搜索。
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.
⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽
⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽
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