diff options
author | jcej <jcej@ae88bc3d-4319-0410-8dbf-d08b4c9d3795> | 1999-01-06 19:30:06 +0000 |
---|---|---|
committer | jcej <jcej@ae88bc3d-4319-0410-8dbf-d08b4c9d3795> | 1999-01-06 19:30:06 +0000 |
commit | 3d62c941ce203469633f5bbbcfc666c07754f09f (patch) | |
tree | 5a81122e423b584f5fde883795606c0c1936dd41 /docs/tutorials/021/page03.html | |
parent | 100360c6c2079c5971aa6a14b282980659083414 (diff) | |
download | ATCD-3d62c941ce203469633f5bbbcfc666c07754f09f.tar.gz |
*** empty log message ***
Diffstat (limited to 'docs/tutorials/021/page03.html')
-rw-r--r-- | docs/tutorials/021/page03.html | 132 |
1 files changed, 132 insertions, 0 deletions
diff --git a/docs/tutorials/021/page03.html b/docs/tutorials/021/page03.html new file mode 100644 index 00000000000..017134a55e5 --- /dev/null +++ b/docs/tutorials/021/page03.html @@ -0,0 +1,132 @@ +<HTML> +<HEAD> + <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1"> + <META NAME="Author" CONTENT="James CE Johnson"> + <TITLE>ACE Tutorial 021</TITLE> +</HEAD> +<BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#000FFF" VLINK="#FF0F0F"> + +<CENTER><B><FONT SIZE=+2>ACE Tutorial 021</FONT></B></CENTER> + +<CENTER><B><FONT SIZE=+2>Pooling your memories</FONT></B></CENTER> + +<P> +<HR WIDTH="100%"> + The client side is a little simpler than the server. Mainly + because we don't try to delete the pool: + <ul> + <li>Create an Allocator to access the pool + <li>Find the named region + </ul> +<hr><PRE> + +<font color=red>// $Id$</font> + +<font color=blue>#include</font> "<font color=green>mpool.h</font>" + +int main (int, char *[]) +{ + <font color=red>/* + Use the same pool name used by the server when we create our + Allocator. This assures us that we don't create a whole new + pool. + */</font> + Allocator allocator(<font color=#008888>Constants::PoolName</font>); + + <font color=red>/* + You can put anything in the memory pool. Not just the + character array we want. The find() method till, therefore, + return a void* that we will have to cast. + */</font> + void * region; + + <font color=red>/* + We use find() to locate a named region in the pool. This is + the counterpart to bind() used in the server. + Here, we go try to find the region that the server has created + and filled with data. If there was a problem getting the pool + or finding the region, we'll get back -1 from find(). + */</font> + if( allocator.pool().find(<font color=#008888>Constants::RegionName</font>,region) == -1 ) + { + ACE_ERROR_RETURN ((LM_ERROR, "<font color=green>Cannot find the name '%s'\n</font>", + <font color=#008888>Constants::RegionName</font>), 100 ); + } + + <font color=red>/* + Since find() returns us a void*, we cast it here to the char* + that we want. + */</font> + char *shm = (char *)region; + + ACE_DEBUG ((LM_INFO, "<font color=green>Shared memory is at 0x%x\n</font>", shm )); + + <font color=red>/* + The same pair of semaphores as used by the server are created + here. We probably don't need the CREATE flag since the server + should have already done that. There may be some very small + windows, however, where the server would have created the + memory pool but not yet gotten to the semaphores. + */</font> + ACE_SV_Semaphore_Complex mutex; + ACE_ASSERT (mutex.open (<font color=#008888>Constants::SEM_KEY_1</font>, + <font color=#008888>ACE_SV_Semaphore_Complex::ACE_CREATE</font>, 0) != -1); + + ACE_SV_Semaphore_Complex synch; + ACE_ASSERT (synch.open (<font color=#008888>Constants::SEM_KEY_2</font>, + <font color=#008888>ACE_SV_Semaphore_Complex::ACE_CREATE</font>, 0) != -1); + + <font color=red>/* + It doesn't matter if we created 'mutex' or if the server did. + In either case, it was created in a locked state and we will + block here until somebody unlocks it. In our scenario, that + will have to be the server. + */</font> + if (mutex.acquire () == -1) + { + ACE_ERROR_RETURN ((LM_ERROR, "<font color=green>(%P) client mutex.acquire</font>"), 1); + } + + <font color=red>/* + Now that we know it is safe to access the data, we'll run + through and make sure that it contains what we think the server + supplied. + */</font> + for (int i = 0; i < <font color=#008888>Constants::SHMSZ</font>; i++) + { + ACE_ASSERT (<font color=#008888>Constants::SHMDATA</font>[i] == shm[i]); + } + + <font color=red>/* + Look back at the server. After filling the region, it will + attempt to acquire the lock on 'synch'. It will wait there + until we release() the semaphore. That will allow it to remove + the pool and cleanup. We can simply exit once we perform the + release. (Ok, a free() of the region would probably be polite...) + */</font> + if (synch.release () == -1) + { + ACE_ERROR_RETURN ((LM_ERROR, "<font color=green>(%P) client synch.release</font>"), 1); + } + + return 0; +} + +<font color=red>/* + Again, we have the necessary explicit template instantiations + because I based this on an ACE example instead of creating it from scratch. + */</font> +<font color=blue>#if defined</font> (<font color=purple>ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION</font>) +template class ACE_Malloc<ACE_MMAP_MEMORY_POOL, ACE_SV_Semaphore_Simple>; +template class ACE_Guard<ACE_SV_Semaphore_Simple>; +template class ACE_Write_Guard<ACE_SV_Semaphore_Simple>; +template class ACE_Read_Guard<ACE_SV_Semaphore_Simple>; +<font color=blue>#elif defined</font> (<font color=purple>ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA</font>) +<font color=blue>#pragma</font> <font color=purple>instantiate</font> ACE_Malloc<ACE_MMAP_MEMORY_POOL, ACE_SV_Semaphore_Simple> +<font color=blue>#pragma</font> <font color=purple>instantiate</font> ACE_Guard<ACE_SV_Semaphore_Simple> +<font color=blue>#pragma</font> <font color=purple>instantiate</font> ACE_Write_Guard<ACE_SV_Semaphore_Simple> +<font color=blue>#pragma</font> <font color=purple>instantiate</font> ACE_Read_Guard<ACE_SV_Semaphore_Simple> +<font color=blue>#endif</font> <font color=red>/* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION */</font> +</PRE> +<P><HR WIDTH="100%"> +<CENTER>[<A HREF="../online-tutorials.html">Tutorial Index</A>] [<A HREF="page04.html">Continue This Tutorial</A>]</CENTER> |