Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy are pthreads executing sequentially in c
    text
    copied!<p>I'm creating 10 pthreads in c and it seems that they are running sequentially for some reason. I've assigned an id to each thread and I'm writing the id to the array 10 times for each thread. Once the 10 threads are done writing, I parse the array. When going through the array, the id for each thread appears consecutively. There are 100 ids being written to the array and they're all being written consecutively. What I'm expecting is race conditions to occur so that some id's are overwritten. I'm also not expecting all the ids to appear consecutively in the array.</p> <p>Here's the code:</p> <pre><code>#include &lt;stdlib.h&gt; #include &lt;stdio.h&gt; #include &lt;pthread.h&gt; //Count variabes and flags int noItemsToProduce = 10; //Number of items produced by a single thread. int totalItemsToProduce = 100; //Number of items produced by 10 threads. int noItemsConsumed = 0; //Number of items removed by consumer. int done = 0; //Flag used to know when to terminate. int queueEmpty = 0; void *res; //Stores the result from the thread. //Queue Variables int *queueArray; int front = 0; int back = -1; void enqueue(int item) { if(back &lt; totalItemsToProduce-1) { queueArray[++back] = item; } } void dequeue() { if(front&lt;=back) { front++; } else { queueEmpty = 1; front=0; back=-1; } } static void *producer(void *arg) { int i; int id = strtol(arg,NULL,0); for(i=0;i&lt;noItemsToProduce;i++) { enqueue(id); } } static void *consumer(void *arg) { while(!done || !queueEmpty) { printf("Dequeuing item with id = %d at pos = %d.\n",queueArray[front],front); dequeue(); noItemsConsumed++; } } int main(int argc,char *argv[]) { //The user needs to specify the number of ints each of the 10 //producers will produce. if (argc!=2) { printf("Usage: %s #-items-for-each-producer\n",argv[0]); return -1; } else { noItemsToProduce = strtol(argv[1],NULL,0); totalItemsToProduce = 10*noItemsToProduce; queueArray = (int *)malloc(sizeof(int)*totalItemsToProduce); } //Declarations. queueArray = (int *)malloc(sizeof(int)*(10*noItemsToProduce)); pthread_t p1; pthread_t p2; pthread_t p3; pthread_t p4; pthread_t p5; pthread_t p6; pthread_t p7; pthread_t p8; pthread_t p9; pthread_t p10; pthread_t c; //Start producer and consumer threads. pthread_create(&amp;p1,NULL,producer,"1"); pthread_create(&amp;p2,NULL,producer,"2"); pthread_create(&amp;p3,NULL,producer,"3"); pthread_create(&amp;p4,NULL,producer,"4"); pthread_create(&amp;p5,NULL,producer,"5"); pthread_create(&amp;p6,NULL,producer,"6"); pthread_create(&amp;p7,NULL,producer,"7"); pthread_create(&amp;p8,NULL,producer,"8"); pthread_create(&amp;p9,NULL,producer,"9"); pthread_create(&amp;p10,NULL,producer,"10"); //Wait until all of the producers finish producing. pthread_join(p1,&amp;res); pthread_join(p2,&amp;res); pthread_join(p3,&amp;res); pthread_join(p4,&amp;res); pthread_join(p5,&amp;res); pthread_join(p6,&amp;res); pthread_join(p7,&amp;res); pthread_join(p8,&amp;res); pthread_join(p9,&amp;res); pthread_join(p10,&amp;res); //We're done producing so let the consumer know. done = 1; pthread_create(&amp;c, NULL, consumer, queueArray); pthread_join(c,&amp;res); printf("Total items produced = %d.\n",10*noItemsToProduce); printf("Total items consumed = %d.\n",noItemsConsumed-1); return 0; } </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