c++ - pthread_join 期间的段错误

标签 c++ segmentation-fault pthreads

#define SIZE 3 
#define MAX_THREADS 9 
int main() {

    ...

    pthread_t m_threads[MAX_THREADS];
    int t_num = 0;

    for (int i = 0; i < SIZE; ++i) {
        for (int j = 0; j < SIZE; ++j) {

            ...

            pthread_create(&m_threads[i], NULL, multiply, (void *) &td[t_num]);
            t_num++;
        }
    }

    for( int i = 0; i < MAX_THREADS; i++ ) {
        pthread_join(m_threads[i], NULL);
    }

    return 0;
}

我正在创建 9 个线程来做一些矩阵乘法。计算成功。但是当我尝试加入线程时程序崩溃了。程序在执行 pthread_join(m_threads[i], NULL) 时崩溃在 for 循环的第 4 次迭代中。

调试程序我从 GDB 得到这个段错误。

Thread 1 "Pthread" received signal SIGSEGV, Segmentation fault.
0x00007f7d0063ac9f in __free_tcb () from /lib/x86_64-linux-gnu/libpthread.so.0

Program terminated with signal SIGSEGV, Segmentation fault.
The program no longer exists.

最佳答案

修改pthread_create(&m_threads[i]pthread_create(&m_threads[t_num]解决了这个问题。

关于c++ - pthread_join 期间的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60071431/

相关文章:

c - 检查地址范围是否在进程地址空间中的石蕊测试?

C 段错误 :11

消费者生产者与 pthreads 有竞争条件

c++ - 用户定义的字符串文字和模式与 sscanf 匹配

c++ - 如何在OpenGL中拥有多个独立移动的对象

c++ - 没有匹配函数调用 ‘Board::Board()’

c++ - 运行时计数器退出代码 139,但 gdb 使其通过

c++ - 函数签名的专用模板

linux - 获取当前的cpu信息

c - 不带参数的 pthread_create 函数