Note that there are some explanatory texts on larger screens.

plurals
  1. POC pthread and struct questions
    primarykey
    data
    text
    <p>Basic function of this code is to get number of counter and thread, creates the counters then create the threads then get numbers of instructions in each thread (format of instruction [counter] [work-function] [repetition] )</p> <pre><code>/* ============================================================================ * File-global variables * ========================================================================== */ static int ncounters = 0; static struct counter *counters = NULL; static int nthreads = 0; static int *ninstructions = NULL; static struct instruction **instructions = NULL; struct counter { long long counter; /* to store counter */ }; /* counter value */ struct instruction { struct counter *counter; /* pointer to counter */ int repetitions; /* number of repetitions */ void (*work_fn)(long long *); /* function pointer to work function */ }; /* ============================================================================ * Thread function * ========================================================================== */ static void * worker_thread(void *arg) { (void)arg; int Tcounter; int Trepetition; char Tfuntion; scanf(" %d %c %d", &amp;Tcounter, &amp;Tfuntion, &amp;Trepetition); </code></pre> <p>How do I actually store this three variable using struct instruction **instructions???</p> <pre><code> return NULL; } /* ============================================================================ * Main function * ========================================================================== */ int main(void) { scanf(" %d", &amp;ncounters); int i; if(counters = malloc(sizeof(struct counter)*ncounters)){ for( i=0; i &lt; ncounters ;i++){ counters[i].counter = 0; } for( i=0; i &lt; ncounters ;i++){ printf("%lld\n", counters[i].counter); } } scanf(" %d", &amp;nthreads); pthread_t threads[nthreads]; int ninst; for( i=0; i &lt; nthreads ;i++){ scanf(" %d", &amp;ninst); ninstructions = ninst; for( i=0; i &lt; ninstructions ;i++){ pthread_create(&amp;threads[i], NULL, worker_thread, NULL); } } free(counters); return 0; } </code></pre> <p>Is the pthread_create function correct?</p> <pre><code>int main(void) { scanf(" %d", &amp;ncounters); int i; if(counters = malloc(sizeof(struct counter)*ncounters)){ for( i=0; i &lt; ncounters ;i++){ counters[i].counter = 0; } } scanf(" %d", &amp;nthreads); pthread_t threads[nthreads]; if(ninstructions = (int*)malloc(nthreads*sizeof(int)){ for( i=0; i &lt; nthreads ;i++){ scanf(" %d", &amp;ninstructions[i]); int j; for(j=0; i &lt; ninstructions[i] ;j++){ pthread_create(&amp;threads[j], NULL, worker_thread, NULL); } } } free(ninstructions); free(counters); return 0; } </code></pre> <p>I also tried to change the ninstruction into array, if its correct...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload