Note that there are some explanatory texts on larger screens.

plurals
  1. POPOSIX C Threads. pthread_cond_t example. Doesn't work as expected
    primarykey
    data
    text
    <p>I wrote a wrote a program and it doesn't work as I expect it to. I have two threads: <code>thread</code> triggers <code>func</code> and <code>anotherThread</code> triggers <code>anotherFunc</code>. What I wanted to do is when <code>cont</code> reaches value <code>10</code> in <code>func</code>, <code>anotherThread</code> to be triggered using <code>pthread_cond_wait</code> and <code>pthread_cond_signal</code>. The strange thing is everything works fine if i uncomment the <code>sleep(1)</code> line. I'm new to threads and I was following the tutorial <a href="https://computing.llnl.gov/tutorials/pthreads/#ConditionVariables" rel="noreferrer">here</a> and if I comment the <code>sleep</code> line in their example it breaks as well. </p> <p>My question is how can I make this work without any <code>sleep()</code> calls? And what happens if in my code both <code>func</code> reaches <code>pthread_mutex_lock</code> after <code>anotherFunc</code>? How can I control these things? This is my code:</p> <pre><code>#include &lt;iostream&gt; #include &lt;stdlib.h&gt; #include &lt;stdio.h&gt; #include &lt;pthread.h&gt; pthread_mutex_t myMutex; pthread_cond_t cond; pthread_attr_t attr; int cont; void *func(void*) { printf("func\n"); for(int i = 0; i &lt; 20; i++) { pthread_mutex_lock(&amp;myMutex); cont++; printf("%d\n", cont); if(cont == 10) { printf("signal:\n"); pthread_cond_signal(&amp;cond); // sleep(1); } pthread_mutex_unlock(&amp;myMutex); } printf("Done func\n"); pthread_exit(NULL); } void *anotherFunc(void*) { printf("anotherFunc\n"); pthread_mutex_lock(&amp;myMutex); printf("waiting...\n"); pthread_cond_wait(&amp;cond, &amp;myMutex); cont += 10; printf("slot\n"); pthread_mutex_unlock(&amp;myMutex); printf("mutex unlocked anotherFunc\n"); printf("Done anotherFunc\n"); pthread_exit(NULL); } int main(int argc, char *argv[]) { pthread_t thread; pthread_t anotherThread; pthread_attr_init(&amp;attr); pthread_mutex_init(&amp;myMutex, NULL); pthread_attr_setdetachstate(&amp;attr, PTHREAD_CREATE_JOINABLE); pthread_cond_init(&amp;cond, NULL); pthread_create(&amp;anotherThread, &amp;attr, anotherFunc, NULL); pthread_create(&amp;thread, &amp;attr, func, NULL); pthread_join(thread, NULL); pthread_join(anotherThread, NULL); printf("Done MAIN()"); pthread_mutex_destroy(&amp;myMutex); pthread_cond_destroy(&amp;cond); pthread_attr_destroy(&amp;attr); pthread_exit(NULL); return 0; } </code></pre> <p>Sorry for the long post but I'm new to threads and I'm willing to learn. Also do you know some good references or courses/tutorials on threads and networking on Linux? I want to learn create an chat client and I heard that I have to know threads and networking for that. Problem is I don't know pretty good if what I learn is ok since I don't know what I have to know.</p> <p>Thank you so much :)</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.
 

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