/* 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 #include #include #include 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 */