title: Threads in C - Textbook answer about multithreading
body: <p>I have pulled a question from <a href="https://rads.stackoverflow.com/amzn/click/com/0133805913" rel="nofollow noreferrer" rel="nofollow noreferrer">Operating Systems - Internals and Design Principles Eighth Edition</a>.</p>
  
  <p>I have tried my answer but really would like to know the proper response to this question.</p>
  
  <blockquote>
    <p>Considering the following C declarations and function definition:</p>
  
  <pre><code>int global_positives = 0;
  typedef struct list { 
     struct list *next;
     double val;
  } *list;
  
  void count_positives(list l)
  {
    list p;
    for (p = 1; p; p = p -&gt; next)
       if (p -&gt; val &gt; 0.0)
          ++global_positives;
  }
  </code></pre>
    
    <p>While thread A does:</p>
  
  <pre><code>count_positives(&lt;list containing only negative numbers&gt;);
  </code></pre>
    
    <p>and thread B does:</p>
  
  <pre><code>++global_positives;
  </code></pre>
    
    <p>a) what does the function do?<br>
    b) The C language only addresses single threaded execution. Does the use of two parallel threads create any problems?</p>
  </blockquote>
  
  <p>So my answers I would like to know if correct are:</p>
  
  <p>a) The function as of now will do nothing in the case of thread A calling the function with only negative numbers since nothing will be greater than zero as well as...</p>
  
  <p>b) the problem of having two threads run in parallel in C because since only one can run at a time global_positives will never increment and the answer will always be zero.</p>
  
  <p>In the ballpark?</p>