How to use mutex in C for multithread?
<p>I have 4 processed called A, B, C, D in 4 thread, They printf their name. I want use mutex to process A, B, C, D run in order A, B, C, D. This is my code but it don't work such as me think. How to they work?</p>

<pre><code>#include &lt;stdio.h&gt;
#include &lt;pthread.h&gt;

void processA();
void processB();
void processC();
void processD();

pthread_mutex_t mutex;

void main(){
    pthread_t thread1;
    pthread_t thread2;
    pthread_t thread3;
    pthread_t thread4;

    pthread_mutex_init(&amp;mutex,NULL);

    pthread_create(&amp;thread1, NULL, (void *)&amp;processA,NULL);
    pthread_create(&amp;thread2, NULL, (void *)&amp;processB,NULL);
    pthread_create(&amp;thread3, NULL, (void *)&amp;processC,NULL);
    pthread_create(&amp;thread4, NULL, (void *)&amp;processD,NULL);

    pthread_join(thread1, NULL);
    pthread_join(thread2, NULL);
    pthread_join(thread3, NULL);
    pthread_join(thread4, NULL);            

    pthread_mutex_destroy(&amp;mutex);
}
void processA()
{
    while (1)
    {
        pthread_mutex_lock(&amp;mutex);
        printf("A \n");
        pthread_mutex_unlock(&amp;mutex);
    }
}
void processB()
{
    while (1)
    {
        pthread_mutex_lock(&amp;mutex);
        printf("B \n");
        pthread_mutex_unlock(&amp;mutex);
    }
}
void processC()
{
    while (1)
    {
        pthread_mutex_lock(&amp;mutex);
        printf("C \n");
        pthread_mutex_unlock(&amp;mutex);
    }
}
void processD()
{
pthread_mutex_lock(&amp;mutex);
    while (1)
    {
        pthread_mutex_lock(&amp;mutex);
        printf("D \n");
        pthread_mutex_unlock(&amp;mutex);
    }
}
</code></pre>