c - What is a good way to stop all pthreads without using mutex or global variables? -


say hypothetically not know mutex_locks , not allowed use global variables in program, can in order stop running pthreads if 1 returns successful variable?

for example have data structure pass pthread contains :

typedef struct {      char * string1;         //info argv[1]     char * string2;         //info argv[2]      int id;                 //thread id  } datastruct; 

and while creating pthreads in main.c create them such :

datastruct datastr[nbthread];           //array of datastructs each thread pthread_t tabthread[nbthread];          //pointers thread 1  (int = 0; < nbthread; ++i) {    //initiation of threads...     datastr[i].string1 = argv[1];      pthread_create(&tabthread[i], null, thread, (void *)&datastr[i]);   //create pthreads , }     (int = 0; < nbthread; ++i) {     pthread_join(tabthread[i], (void**)&(ptr[i]));                      //join threads     //printf("\n return value thread[%d] [%d]\n",i, *ptr[i]); } 

now 1 of threads finds hoping achieve, how can threads stop simultaneously?

can have pointer in struct points variable in main can changed true thread successful?

can use return value pthreads somehow stop them all?

i'm having bit of difficulty understanding pointers. appreciated.

this how accomplish wish posix threads:

#include <unistd.h> #include <stdbool.h> #include <pthread.h> #include <stdio.h> #include <stdlib.h>  #define num_threads 10  pthread_mutex_t cv_mutex; pthread_cond_t notification_cv; pthread_t threads[num_threads];  bool allfinished = false;  void *worker(void *arg) {     /* allow cancellation, */     pthread_setcancelstate(pthread_cancel_enable, null);     /* , canceled @ time, not @ cancellation points: */     pthread_setcanceltype(pthread_cancel_asynchronous, null);      int loop = 0;      while (true) {         printf("iteration %d\n", loop);         ++loop;          /* "work": */         ;          /* assume shared state          * condition variable, , workers          * share no other state.          * if do, adjust critical section accordingly,          * perhaps 2nd mutex.          */         pthread_mutex_lock(&cv_mutex);         if (loop > 5) { /* simulate "work end" */             allfinished = true;             pthread_cond_broadcast(&notification_cv);             pthread_mutex_unlock(&cv_mutex);             pthread_exit(null);         }         pthread_mutex_unlock(&cv_mutex);     }     pthread_exit(null); }  void *master(void *t) {     /* lock mutex , wait signal. */     pthread_mutex_lock(&cv_mutex);      /* loop because thread might awoken      * waiting state though no thread signalled      * condition variable:      */     while (!allfinished)         pthread_cond_wait(&notification_cv, &cv_mutex);     printf("master: woken up.\n");      /* unlocking mutex allows workers signal condition variable again,      * irrelevant we're heading end-of-life:      */     pthread_mutex_unlock(&cv_mutex);      (size_t = 0; < num_threads - 1; ++i) {         pthread_cancel(threads[i]);     }      pthread_exit(null); }  int main(int argc, char *argv[]) {     pthread_attr_t attr;      /* initialize mutex , condition variable objects */     pthread_mutex_init(&cv_mutex, null);     pthread_cond_init(&notification_cv, null);      /* portability, explicitly create threads in joinable state: */     pthread_attr_init(&attr);     pthread_attr_setdetachstate(&attr, pthread_create_joinable);      /* keep separate `master` thread, may doing other bookkeeping: */     pthread_t s;     pthread_create(&s, &attr, master, null);      for(size_t = 0; < sizeof threads / sizeof *threads; ++i) {         if (!pthread_create (&threads[i], &attr, worker, null) ) {             printf("worker %zu created\n", i);         }     }      /* wait threads complete. not wait forever because master      * thread cancel of workers:      */     (size_t = 0; < sizeof threads / sizeof *threads; ++i) {         pthread_join(threads[i], null);         printf("worker %zu done\n", i);     }     pthread_join(s, null);     printf("master done\n");      /* clean , exit */     pthread_attr_destroy(&attr);     pthread_mutex_destroy(&cv_mutex);     pthread_cond_destroy(&notification_cv);     pthread_exit(null); } 

Comments

Popular posts from this blog

php - How to add and update images or image url in Volusion using Volusion API -

javascript - jQuery UI Splitter/Resizable for unlimited amount of columns -

javascript - IE9 error '$'is not defined -