Note that there are some explanatory texts on larger screens.

plurals
  1. POMultithreading
    primarykey
    data
    text
    <p>I have just started learning multi-threading. I have written a simple application. The application creates three threads. Two threads write and one thread reads. The writer threads write to separate location in a global array. The writer thread after incrementing the value in the array notifies the reader. The reader thread then decrements that value in the array and waits again for the writer threads to update their corresponding value in the array. The code for the application is pasted below.</p> <p>What I see is that the writer(Producer) threads get more time slice than the reader(Consumer) thread. I think I am doing something wrong. If the output of the application is redirected to a file, then it can be observed that there are more consecutive messages from the Producers and the messages from the Consumer occur infrequently. What I was expecting was that, when a Producer updates its data, the Consumer immediately processes it i.e. after every Producer message there should be a Consumer message printed.</p> <p>Thanks and regards,</p> <p>~Plug</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;pthread.h&gt; const long g_lProducerCount = 2; /*Number of Producers*/ long g_lProducerIds[2]; /*Producer IDs = 0, 1...*/ long g_lDataArray[2]; /*Data[0] for Producer 0, Data[1] for Producer 1...*/ /*Producer ID that updated the Data. -1 = No update*/ long g_lChangedProducerId = -1; pthread_cond_t g_CondVar = PTHREAD_COND_INITIALIZER; pthread_mutex_t g_Mutex = PTHREAD_MUTEX_INITIALIZER; pthread_t g_iThreadIds[3]; /*3 = 2 Producers + 1 Consumer*/ unsigned char g_bExit = 0; /*Exit application? 0 = No*/ void* Producer(void *pvData) { long lProducerId = *(long*)pvData; /*ID of this Producer*/ while(0 == g_bExit) { pthread_mutex_lock(&amp;g_Mutex); /*Tell the Consumer who's Data is updated*/ g_lChangedProducerId = lProducerId; /*Update the Data i.e. Increment*/ ++g_lDataArray[lProducerId]; printf("Producer: Data[%ld] = %ld\n", lProducerId, g_lDataArray[lProducerId]); pthread_cond_signal(&amp;g_CondVar); pthread_mutex_unlock(&amp;g_Mutex); } pthread_exit(NULL); } void* Consumer(void *pvData) { while(0 == g_bExit) { pthread_mutex_lock(&amp;g_Mutex); /*Wait until one of the Producers update it's Data*/ while(-1 == g_lChangedProducerId) { pthread_cond_wait(&amp;g_CondVar, &amp;g_Mutex); } /*Revert the update done by the Producer*/ --g_lDataArray[g_lChangedProducerId]; printf("Consumer: Data[%ld] = %ld\n", g_lChangedProducerId, g_lDataArray[g_lChangedProducerId]); g_lChangedProducerId = -1; /*Reset for next update*/ pthread_mutex_unlock(&amp;g_Mutex); } pthread_exit(NULL); } void CreateProducers() { long i; pthread_attr_t attr; pthread_attr_init(&amp;attr); for(i = 0; i &lt; g_lProducerCount; ++i) { g_lProducerIds[i] = i; pthread_create(&amp;g_iThreadIds[i + 1], &amp;attr, Producer, &amp;g_lProducerIds[i]); } pthread_attr_destroy(&amp;attr); } void CreateConsumer() { pthread_attr_t attr; pthread_attr_init(&amp;attr); pthread_create(&amp;g_iThreadIds[0], &amp;attr, Consumer, NULL); pthread_attr_destroy(&amp;attr); } void WaitCompletion() { long i; for(i = 0; i &lt; g_lProducerCount + 1; ++i) { pthread_join(g_iThreadIds[i], NULL); } } int main() { CreateProducers(); CreateConsumer(); getchar(); g_bExit = 1; WaitCompletion(); return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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