Note that there are some explanatory texts on larger screens.

plurals
  1. POCan threads write to different elements of same array of structures without locking?
    text
    copied!<p>I'm trying to use threads (for the first time!) in a GCC C application which works fine in non-threaded mode. When I run it some threads give results which are all zero instead of the required answers (which I know for checking purposes), but the threads giving zeroes are not the same each time I run it. The ones which give non-zero answers are correct, so the code appears to run ok as such. I wonder if anyone can point out areas where I might have something which is not thread-safe.</p> <p>My own thoughts are it may be due to how I collect results or maybe memory allocation - I use malloc and free but elsewhere in StackOverflow I see that GCC malloc is considered thread-safe if linked with -lpthread (which I am doing). Nothing uses global/static variables - everything is passed as function arguments.</p> <p>In order to pass results back to main, my threaded routine uses an array of structures. Each thread writes to a distinct element of this array, so they are not trying to write to the same memory. Maybe I need to use some form of locking when writing results even though they don't go to the same element of the structure array?</p> <p>I followed the recipe for threaded code here: <a href="https://computing.llnl.gov/tutorials/pthreads/#Abstract" rel="noreferrer">https://computing.llnl.gov/tutorials/pthreads/#Abstract</a></p> <p>I attach (simplified) code extracts in case this gives any clues (I may have omitted/modified something incorrectly but I am not asking for anyone to spot bugs, just the general methodology).</p> <pre><code>typedef struct p_struct { /* used for communicating results back to main */ int given[CELLS]; int type; int status; /*... etc */ } puzstru; typedef struct params_struct { /* used for calling generate function using threads */ long seed; char *text; puzzle *puzzp; bool unique; int required; } paramstru; /* ========================================================================================== */ void *myfunc(void *spv) /* calling routine for use by threads */ { paramstru *sp=(paramstru *)spv; generate(sp-&gt;seed, sp-&gt;text, sp-&gt;puzzp, sp-&gt;unique, sp-&gt;required); pthread_exit((void*) spv); } /* ========================================================================================== */ int generate(long seed, char *text, puzstru *puzzp, bool unique, int required) { /* working code , also uses malloc and free, puts results in the element of a structure array pointed to by "puzzp", which is different for each thread (see calling routine below : params-&gt;puzzp=puz+thr; ) extract as follows: */ puzzp-&gt;given[ix]=calcgiven[ix]; puzzp-&gt;type=1; puzzp-&gt;status=1; /* ... etc */ } /* ========================================================================================== */ int main(int argc, char* argv[]) { pthread_t thread[NUM_THREADS]; pthread_attr_t threadattr; int thr,threadretcode; void *threadstatus; paramstru params[1]; /* ....... ETC */ /* set up params structure for function calling parameters */ params-&gt;text=mytext; params-&gt;unique=TRUE; params-&gt;required=1; /* Initialize and set thread detached attribute */ pthread_attr_init(&amp;threadattr); pthread_attr_setdetachstate(&amp;threadattr, PTHREAD_CREATE_JOINABLE); for(thr=0; thr&lt;NUM_THREADS; thr++) { printf("Main: creating thread %d\n", thr); params-&gt;seed=ran_arr_next(startingseeds); params-&gt;puzzp=puz+thr; threadretcode = pthread_create(&amp;thread[thr], &amp;threadattr, myfunc, (void *)params); if (threadretcode) { printf("ERROR; return code from pthread_create() is %d\n", threadretcode); exit(-1); } } /* Free thread attribute and wait for the other threads */ pthread_attr_destroy(&amp;threadattr); for(thr=0; thr&lt;NUM_THREADS; thr++) { threadretcode = pthread_join(thread[thr], &amp;threadstatus); if (threadretcode) { printf("ERROR; return code from pthread_join() is %d\n", threadretcode); exit(-1); } printf("Main: completed join with thread %d having a status of %ld\n",thr,(long)threadstatus); } /* non-threaded code, print results etc ............. */ free(startingseeds); free(puz); printf("Main: program completed. Exiting.\n"); pthread_exit(NULL); } </code></pre> <p>For the benefit of others reading this - all the answers were correct, and the answer to the question in the heading is YES, threads can write safely to different elements of the same array of structures, my problem was in the calling routine - the following is the amended code snippet (now works fine):</p> <pre><code> paramstru params[NUM_THREADS]; for(thr=0; thr&lt;NUM_THREADS; thr++) { printf("Main: creating thread %d\n", thr); /* set up params structure for function calling parameters */ params[thr].text=mytext; params[thr].unique=TRUE; params[thr].required=1; params[thr].seed=ran_arr_next(startingseeds); params[thr].puzzp=puz+thr; threadretcode = pthread_create(&amp;thread[thr], &amp;threadattr, myfunc, (void *)&amp;params[thr]); if (threadretcode) { printf("ERROR; return code from pthread_create() is %d\n", threadretcode); exit(-1); } } </code></pre>
 

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