2012-04-15 6 views
8

私は大きな問題があります。私が期待しているように、Cのミューテックスがなぜうまくいかないのか分かりません。 これは私のコードです:私は起こることを期待何POSIX Cスレッド。ミューテックスの例。期待どおりに動作しない

#include <stdlib.h> 
#include <stdio.h> 
#include <pthread.h> 

pthread_t mythread; 
pthread_mutex_t mymutex; 

void *anotherFunc(void*) 
{ 
    pthread_mutex_lock(&mymutex); 

    for(int i = 0; i < 100; i++) 
     printf("anotherFunc\n"); 

    pthread_mutex_unlock(&mymutex); 

    pthread_exit(NULL); 
} 

void *func(void*) 
{ 
    pthread_mutex_lock(&mymutex); 

    for(int i = 0; i < 100; i++) 
     printf("func\n"); 

    pthread_mutex_unlock(&mymutex); 

    pthread_exit(NULL); 
} 

int main(int argc, char *argv[]) 
{ 
    pthread_mutex_init(&mymutex, NULL); 

    pthread_create(&mythread, NULL, func, NULL); 
    pthread_create(&mythread, NULL, anotherFunc, NULL); 

    pthread_mutex_destroy(&mymutex); 

    pthread_exit(NULL); 
    return EXIT_SUCCESS; 
} 

は、最初の100「FUNC」のメッセージと、その後100「anotherFunc」のメッセージを印刷するためのプログラムです。私が期待するのは、実行がfuncに達してmutexをロックすることです。実行がanotherFuncに達すると、私はfuncがmutexのロックを解除するまで待つことを期待しています。しかし、私は

FUNC FUNC FUNC anotherFunc anotherFunc anotherFunc FUNC anotherFunc

のような干渉のメッセージを取得し、私はこのことがどのように機能するかを理解していません。助けてください!

答えて

16
pthread_create(&mythread, NULL, func, NULL); 
pthread_create(&mythread, NULL, anotherFunc, NULL); 

pthread_mutex_destroy(&mymutex); 

あなたは前のスレッドがそれで行われ、その全てのベットがオフになっているミューテックスを破壊しています。おそらく、破棄する前に2スレッドをpthread_joinにすることをお勧めします。

+0

ためでpthread_join()関数を使用することができ、このように私はそれがうまくいくと思う:) これは、興味のある人のための私の最終的なコードです... http://pastebin.me/bc23773578d79a55882d7ced4e04b026 –

+0

まだ壊れています。あなたはmutexを破壊するときに* one *スレッドが終了したことだけを確認します。もう1つはまだ行われていない場合はどうなりますか?どういうわけか強制しない限り、スレッドが起動したり、ミューテックスを獲得したり、特定の順序で終了するという保証はありません。 (マルチスレッディングのレッスン1は、次のようなものです:物事が確実にその順序で起こるようにするには、特定の順序で起こることが保証されています) –

+0

はい、しかし、両方の機能が1つのスレッド、つまり唯一のスレッド:mythreadで開始されています。 そうするのは大丈夫ですか、ある機能に1つのスレッドを使用するのはいつも安全ですか? –

2

私はスレッド "FUNC" の引数として ARG引数名を使用しループ

  • ためで私はint型を宣言することができませんでしたいくつかのcomiplationエラーに

    • を得ました「anotherFunc」

    がmutexを破壊する前に、pthread_joinを使用しました。このようにして

    両方のスレッド「FUNC」と「anotherFuncは」

    その実行を完了した後、私は私のミューテックス「mymutex」を破壊しています。また、各スレッドは、今「mythread1」を、独自のスレッドID を持っており、 「mythread2」はので、私は、各スレッド

    #include <stdlib.h> 
    #include <stdio.h> 
    #include <pthread.h> 
    
    pthread_t mythread1, mythread2; 
    pthread_mutex_t mymutex; 
    
    void *anotherFunc(void *arg) 
    { 
        pthread_mutex_lock(&mymutex); 
        int i; 
    
        for(i = 0; i < 100; i++) 
         printf("anotherFunc\n"); 
    
        pthread_mutex_unlock(&mymutex); 
    
        pthread_exit(NULL); 
    } 
    
    void *func(void *arg) 
    { 
        pthread_mutex_lock(&mymutex); 
        int i; 
        for(i = 0; i < 100; i++) 
         printf("func\n"); 
    
        pthread_mutex_unlock(&mymutex); 
    
        pthread_exit(NULL); 
    } 
    
    int main(int argc, char *argv[]) 
    { 
        pthread_mutex_init(&mymutex, NULL); 
    
        pthread_create(&mythread1, NULL, func, NULL); 
        pthread_create(&mythread2, NULL, anotherFunc, NULL); 
    
    
        pthread_join(mythread1, NULL); 
        pthread_join(mythread2, NULL); 
    
        pthread_mutex_destroy(&mymutex); 
    
        return EXIT_SUCCESS; 
    }