linux 线程传参注意事项

场景:

提示:这里简述项目相关背景:

创建10个线程,每个线程中打印创建的id


问题描述

提示:这里描述项目中遇到的问题:

给线程参数直接传 地址,然后在线程中接收数据,会导致传入的值arg值可能被改变,从而使程序异常。

给线程参数直接传 数据,能够正常。

创建10个线程打印 0 - 9:
正常:0 - 9 都能打印一次
异常:0 - 9有些数没打印,有些打印多次

问题代码如下:

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

// 线程函数
void* thread_function(void* arg) {
    long Id = *((long *)arg);
    printf("Hello from id = %ld\n", Id);
    pthread_exit(NULL);
}

int main() {
    // 定义线程 ID 数组
    pthread_t threads[10];
    int rc;

    // 创建 10 个线程
    for (long i = 0; i < 10; i++) {
        //rc = pthread_create(&threads[i], NULL, thread_function, (void *)i);
        rc = pthread_create(&threads[i], NULL, thread_function, &i);
        if (rc) {
            printf("Error: unable to create thread, error code = %d\n", rc);
            exit(-1);
        }
    }

    // 等待所有线程完成
    for (long i = 0; i < 10; i++) {
        rc = pthread_join(threads[i], NULL);
        if (rc) {
            printf("Error: unable to join, error code = %d\n", rc);
            exit(-1);
        }
    }

    printf("Main: program exiting.\n");
    return 0;
}

运行结果:打印数据错误乱
在这里插入图片描述

正常代码如下:

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

// 线程函数
void* thread_function(void* arg) {
    long Id = *((long *)arg);
    printf("Hello from id = %ld\n", Id);
    pthread_exit(NULL);
}

int main() {
    // 定义线程 ID 数组
    pthread_t threads[10];
    int rc;

    // 创建 10 个线程
    for (long i = 0; i < 10; i++) {
        rc = pthread_create(&threads[i], NULL, thread_function, (void *)i);
        //rc = pthread_create(&threads[i], NULL, thread_function, &i);
        if (rc) {
            printf("Error: unable to create thread, error code = %d\n", rc);
            exit(-1);
        }
    }

    // 等待所有线程完成
    for (long i = 0; i < 10; i++) {
        rc = pthread_join(threads[i], NULL);
        if (rc) {
            printf("Error: unable to join, error code = %d\n", rc);
            exit(-1);
        }
    }

    printf("Main: program exiting.\n");
    return 0;
}

运行结果:正常打印 0 - 9
在这里插入图片描述

原因分析:

提示:这里填写问题的分析:
&i 传递的使 i 的地址,在线程创建后如果还未来的及运行,i就继续循环,i++后,线程再运行就会出现这种情况。(线程都是被cpu在某一时间段调度的,调度顺序可能和创建顺序不同)


解决方案:

提示:这里填写该问题的具体解决方案:
线程arg时候,如果传递地址,尽量确保所有线程的 arg都独立,线程没有执行完成不操作arg传递的地址

相关推荐

  1. C++并发:线函数(一)

    2024-07-22 17:26:03       26 阅读
  2. C++并发:线函数(二)

    2024-07-22 17:26:03       27 阅读
  3. QT 多线使用以及注意事项

    2024-07-22 17:26:03       40 阅读
  4. C# 线线池的使用方法、注意事项

    2024-07-22 17:26:03       50 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-22 17:26:03       52 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-22 17:26:03       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-22 17:26:03       45 阅读
  4. Python语言-面向对象

    2024-07-22 17:26:03       55 阅读

热门阅读

  1. C++版OpenCV_03_图像增强

    2024-07-22 17:26:03       18 阅读
  2. opengaussdb在oepnEuler上安装

    2024-07-22 17:26:03       16 阅读
  3. 求助Python字体下载!

    2024-07-22 17:26:03       13 阅读
  4. 如何在 Nginx 中配置访问日志的格式?

    2024-07-22 17:26:03       16 阅读
  5. 精简的力量:目标检测中的模型压缩技术解析

    2024-07-22 17:26:03       16 阅读
  6. 如何用外呼系统提高销售打电话效率

    2024-07-22 17:26:03       16 阅读
  7. 计算机网络之物理层

    2024-07-22 17:26:03       14 阅读
  8. 并发编程核心概念

    2024-07-22 17:26:03       15 阅读
  9. iOS中的MVVM设计模式

    2024-07-22 17:26:03       19 阅读