为什么pthread_join()没有挂起主线程?

时间:2014-08-13 04:24:23

标签: c linux unix

为什么主线程在thread_1和thread_2仍在运行时退出。 我该如何解决这个问题? 这个错误是由多核CPU引起的吗?

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

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

static int i = 1;

void* thread_1(void*);
void* thread_2(void*);

int main(void) {
    pthread_t tid_1;
    pthread_t tid_2;

    pthread_create(&tid_1, NULL, thread_1, (void*)NULL);
    pthread_create(&tid_2, NULL, thread_2, (void*)NULL);
    pthread_join(&tid_1, NULL);
    pthread_join(&tid_2, NULL);

    pthread_mutex_destroy(&mutex);
    pthread_cond_destroy(&cond);

    exit(0);
}

void* thread_1(void* arg) {
    pthread_cleanup_push(pthread_mutex_unlock, &mutex);
    for (i = 1; i < 7; ++i) {
        pthread_mutex_lock(&mutex);
        printf("thread 1: lock %d\n", __LINE__);
        if (i % 3 == 0) {
            printf("thread 1: pre-signal %d\n", __LINE__);
            pthread_cond_signal(&cond);
            printf("thread 1: after-signal %d\n", __LINE__);
            sleep(1);
        }
        pthread_mutex_unlock(&mutex);
        printf("thread 1: unlock %d\n\n", __LINE__);
        sleep(1);
    }
    pthread_cleanup_pop(0);

    return (void*)0;
}

void* thread_2(void* arg) {
    pthread_cleanup_push(pthread_mutex_unlock, &mutex);
    while (i < 7) {
        pthread_mutex_lock(&mutex);
        printf("thread 2: lock %d\n", __LINE__);
        if (i % 3 != 0) {
            printf("thread 2: pre-wait %d\n", __LINE__);
            pthread_cond_wait(&cond, &mutex);
            printf("thread 2: after-wait %d\n", __LINE__);
        }
        pthread_mutex_unlock(&mutex);
        printf("thread 2: unlock %d\n\n", __LINE__);
        sleep(1);
    }
    pthread_cleanup_pop(0);

    return (void*)0;
}

和gdb调试信息:

Reading symbols from mutex05...done.
    (gdb) b 1
    Breakpoint 1 at 0x400ad5: file main.c, line 1.
    (gdb) r
    Starting program: /home/myl/Workspace/unix/mutex05/mutex05 
    [Thread debugging using libthread_db enabled]
    Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".

    Breakpoint 1, main () at main.c:19
    19        pthread_create(&tid_1, NULL, thread_1, (void *)NULL);
    (gdb) n
    [New Thread 0x7ffff77f5700 (LWP 13524)]
    thread 1: lock 36
    20        pthread_create(&tid_2, NULL, thread_2, (void *)NULL);
    (gdb) n
    thread 1: unlock 45

    [New Thread 0x7ffff6df4700 (LWP 13532)]
    thread 2: lock 59
    21        pthread_join(&tid_1, NULL);
    (gdb) n
    thread 2: pre-wait 62
    22        pthread_join(&tid_2, NULL);
    (gdb) n
    thread 1: lock 36
    thread 1: unlock 45

    24        pthread_mutex_destroy(&mutex);
    (gdb) n
    thread 1: lock 36
    thread 1: pre-signal 39
    thread 1: after-signal 41
    25        pthread_cond_destroy(&cond);
    (gdb) n
    thread 1: unlock 45

    thread 2: after-wait 64
    thread 2: unlock 67

    27        exit(0);
    (gdb) n
    thread 2: lock 59
    thread 2: pre-wait 62
    [Thread 0x7ffff6df4700 (LWP 13532) exited]
    [Thread 0x7ffff77f5700 (LWP 13524) exited]
    [Inferior 1 (process 13520) exited normally]

在thread_1和thread_2返回之前执行的pthread_mutex_destroy()

1 个答案:

答案 0 :(得分:3)

尝试

pthread_join(tid_1, NULL);  
pthread_join(tid_2, NULL);

signature需要pthread_t代替pthread_t*

相关问题