Man Linux: Main Page and Category List


       Cpool - LCG Pool inferface


       #include <Cpool_api.h>

       int Cpool_create(int nbwanted, int * nbget);

       int  Cpool_assign(int poolid, void *(*startroutine)(void *), void *arg,
       int timeout);

       int Cpool_next_index(int poolid);

       int Cpool_next_index_timeout(int poolid, int timeout);


       See Cthread corresponding section.


       (Please read the NOTE section)

       Cpool is a layer built upon  Cthread,  the  LCG  Thread  interface.  It
       allows the user to create dedicated pools, and then to assign to one of
       them a given routine to execute.

       The created processes or threads will remain alive, unless the routines
       assigned to are crashing, or explicitly calling an exit statement, like
       exit() or pthread_exit().

       Typical use might be writing a server,  with  a  bunch  of  pre-created
       processes or pools (depending on the environment with which Cthread has
       been compiled), and assign to a given pool a routine  with  the  socket
       file descriptor as argument address.

       In principle Cpool should be compiled with the same relevant flags with
       which Cthread has been.

       int Cpool_create(int nbwanted, int * nbget);

       This method is creating a pool of nbwanted processes or threads. If the
       second  argument,  nbget  ,  is not NULL, its location will contain the
       number of effectively created threads or processes.

       Return value is the pool ID, a number greater or equal to zero,  or  -1
       in case of error.

       int  Cpool_assign(int poolid, void *(*startroutine)(void *), void *arg,
       int timeout);

       This  method  is  assigning  a  routine  to  poolid  as   returned   by
       Cpool_create,  whose  address  is  startroutine  ,  that  have the same
       prototype as every typical routine  in  multithread  programming.  This
       means  that  it  returns  a  pointer,  and  it  gets as entry a pointer
       identified by the arg  parameter.  The  last  argument  is  a  possible
       timeout  ,  in seconds, which will apply if it is greater than zero. If
       it is lower than zero, the assignment will wait forever until a  thread
       is  available.  If  it  is  equal  to  zero,  the  method  will  return
       immediately if no thread is available.

       Return value is 0 if success, or -1 in case of error.

       int Cpool_next_index(int poolid);

       int Cpool_next_index_timeout(int poolid, int timeout);

       Those methods returns that next available thread number  that  will  be
       assigned  if  you  ever  call  Cpool_assign  immediately  after. If you
       specify a timeout lower or equal than zero, then  this  is  a  blocking
       method  until  one  thread  is  available  at least. Those methods, so,
       returns a number greater or equal than zero, and  -1  if  there  is  an


              Arguments passing in a non-thread environment

              Since  a  forked  process  can  only  address its namespace data
              segment, the address of the arguments,  if  any,  valid  in  its
              parent,  will  not  be  directly accessible for the child we are
              talking about.

              This means that Cpool, in  a  non-thread  environment,  have  to
              trace-back  all  the  memory  allocation visible for the parent.
              Then, Cpool is not passing the address of the arguments, but its
              content  to  the  child  through  a  child-parent communication,
              monitored with a simple protocol.

              There are four cases:
                         1.The address is NULL: nothing will be transmitted to
                     the child
                         2.The  address  is  exactly  a  pointer  returned  by
                     malloc() or realloc(): the full  malloced  area  will  be
                         3.The  address  is  somewhere  in  a  memory allocate
                     block: the remaining memory block, e.g. starting from the
                     address up to its end, will be transmitted.
                         4.the  address  do  not point to any memory allocated
                     area: Cpool will assume it is  a  pointer-like  argument,
                     probably  to  some  static  variables,  visible  for  all
                     processes, and will transmit the content  of  the  memory
                     pointed  by the address, assuming it is coded on 64-bits.
              In any case, the user is passing a pointer, and the routine will
              see  a  pointer,  pointing  to  a  (hopefully, see the point 4.,
              listed upper) same-content area.

              Arguments  design  to  work  on  both  thread   and   non-thread

              The  thread  and  non-thread  arguments  can have conceptually a
              different design when dealing with arguments;

              In a thread environment, the routined passed to Cpool_assign, is
              sharing  memory,  so  is allowed to free() the argument, because
              memory is  then  shared.   On  the  contrary,  in  a  non-thread
              environment, this may be a segmentation fault.

              This  means  that  it  is  recommended  to use static variables,
              containing simple value, like an integer (for example: a  socket
              file descriptor), and not allocated memory. If, neverthless, you
              persist to use free() in your routine, you can use the following

              /* ------------------------ */
              /* In the Caller Routine    */
              /* ------------------------ */

              arg = malloc(...);

              if (! Cpool_assign(...)) {
                if (Cthread_environment() != CTHREAD_TRUE_THREAD) {
                  /* Non-Thread environment */
                } else {
                  /* Thread environment     */
                  /* ... do nothing         */
              } else {
                  /* In cany case it is OK  */

              /* ------------------------ */
              /* In the Execution Routine */
              /* ------------------------ */

              void *routine(void *arg) {
                if (Cthread_environment() == CTHREAD_TRUE_THREAD) {
                  /* Thread environment */
                } else {
                  /* Non-Thread environment */
                  /* ... do nothing         */


       #include <Cpool_api.h>
       #include <stdio.h>
       #include <errno.h>

       #define NPOOL 2
       #define PROCS_PER_POOL 2
       #define TIMEOUT 2
       void *testit(void *);

       int main() {
         int pid;
         int i, j;
         int ipool[NPOOL];
         int npool[NPOOL];
         int *arg;

         pid = getpid();

         printf("... Defining %d pools with %d elements each\n",

         for (i=0; i < NPOOL; i++) {
           if ((ipool[i] = Cpool_create(PROCS_PER_POOL,&(npool[i]))) < 0) {
             printf("### Error No %d creating pool (%s)\n",
           } else {
             printf("... Pool No %d created with %d processes\n",

         for (i=0; i < NPOOL; i++) {
           /* Loop on the number of processes + 1 ... */
           for (j=0; j <= npool[i]; j++) {
             if ((arg = malloc(sizeof(int))) == NULL) {
               printf("### Malloc error, errno = %d (%s)\n",
             *arg = i*10+j;
             printf("... Assign to pool %d (timeout=%d) the %d-th routine 0x%x(%d)\n",
                    ipool[i],TIMEOUT,j+1,(unsigned int) testit,*arg);
             if (Cpool_assign(ipool[i], testit, arg, TIMEOUT)) {
               printf("### Can’t assign to pool No %d (errno=%d [%s]) the %d-th routine\n",
             } else {
               printf("... Okay for assign to pool No %d of the %d-th routine\n",
               If (Cthread_environment() != CTHREAD_TRUE_THREAD) {
                 /* Non-thread environment: the child is in principle not allowed */
                 /* to do free himself                                            */

         /* We wait enough time for our threads to terminate... */


       void *testit(void *arg) {
         int caller_pid, my_pid;

         my_pid = getpid();

         caller_pid = (int) * (int *) arg;

         if (Cthread_environment() == CTHREAD_TRUE_THREAD) {
           /* Thread environment : we free the memory */

         printf("... I am PID=%d called by pool %d, try No %d\n",
                my_pid,caller_pid/10,caller_pid - 10*(caller_pid/10));

          * Wait up to the timeout + 1





       LCG Grid Deployment Team