> Thanks for this. If you find your old code, do send it over, we
> don't mind if it's not the standard release, if it works, we'll
> consider using it.

Please see the attachment.

You need to replace file glpenv02.c with a reentrant version and then
build the package as usual. Note that glpsol will not work, because it
does not create the thread-specific data key used in the reentrant
version of glpenv02.c. Mtsamp.c is an example program that works.

Hope this helps.

Andrew Makhorin
/* glpenv02.c (thread local storage) */

/* (reserved for copyright notice) */

#include <pthread.h>
#include "glpenv.h"

/* re-enterable POSIX version */

pthread_key_t _glp_pth_key;
/* must be initialized in the main program */

void tls_set_ptr(void *ptr)
{     pthread_setspecific(_glp_pth_key, ptr);
      return;
}

void *tls_get_ptr(void)
{     void *ptr;
      ptr = pthread_getspecific(_glp_pth_key);
      return ptr;
}

/* eof */
/* mtsamp.c */

/***********************************************************************
*  This is an example program which illustrates how to use GLPK API in
*  multi-threaded application on POSIX platform.
*
*  To run this program use the following command:
*
*     mtsamp.exe mps-file-1 mps-file-2 ... mps-file-n
*
*  Each mps file specified in the command line is processed by separate
*  thread.
***********************************************************************/

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <glpk.h>

void *solve_mps(void *arg)
{     char *fname = arg;
      glp_prob *P;
      P = glp_create_prob();
      if (glp_read_mps(P, GLP_MPS_FILE, NULL, fname) != 0)
      {  fprintf(stderr, "Cannot read mps file `%s'\n", fname);
         return NULL;
      }
      glp_simplex(P, NULL);
      glp_delete_prob(P);
      glp_free_env();
      return NULL;
}

#define MAX_THREADS 20

extern pthread_key_t _glp_pth_key;

int main(int argc, char *argv[])
{     pthread_t h[1+MAX_THREADS];
      int t, ret;
      if (argc < 2)
      {  fprintf(stderr, "At least one mps file must be specified\n");
         exit(EXIT_FAILURE);
      }
      if (argc-1 > MAX_THREADS)
      {  fprintf(stderr, "Too many mps files\n");
         exit(EXIT_FAILURE);
      }
      ret = pthread_key_create(&_glp_pth_key, NULL);
      if (ret != 0)
      {  fprintf(stderr, "Unable to create thread-specific key\n");
         exit(EXIT_FAILURE);
      }
      for (t = 1; t < argc; t++)
      {  ret = pthread_create(&h[t], NULL, solve_mps, argv[t]);
         if (ret != 0)
         {  fprintf(stderr, "Unable to create thread for `%s'\n",
               argv[t]);
            exit(EXIT_FAILURE);
         }
      }
      for (t = 1; t < argc; t++)
      {  ret = pthread_join(h[t], NULL);
         if (ret != 0)
         {  fprintf(stderr, "Waiting failure for thread %d\n", t);
            exit(EXIT_FAILURE);
         }
      }
      fprintf(stderr, "GLPK multi-threaded DLL seems to work\n");
      return 0;
}

/* eof */
_______________________________________________
Help-glpk mailing list
Help-glpk@gnu.org
http://lists.gnu.org/mailman/listinfo/help-glpk

Reply via email to