gpt4 book ai didi

c - 为什么 pthread_join() 不挂起主线程?

转载 作者:太空宇宙 更新时间:2023-11-04 07:15:25 24 4
gpt4 key购买 nike

为什么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()

最佳答案

尝试

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

signature采用 pthread_t 而不是 pthread_t*

关于c - 为什么 pthread_join() 不挂起主线程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25278027/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com