c - 如何将多个参数传递给线程函数

标签 c unix ubuntu posix

我已经为线程创建了一个函数,但我想将多个参数传递给该函数。

这是我的源代码:

#include "work.h"
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>    // compile with -lpthread

int count = 20;

void* ChildProc(void* arg)
{
    int i;

    for(i = 1; i <= count; i++)
    {   
        printf("%s:%d from thread <%x>\n", arg, i, pthread_self());
        DoWork(i);
    }

    return NULL;
}

void ParentProc(void)
{
    int i;

    for(i = count / 2; i > 0; i--)
    {
        printf("Parent:%d from thread <%x>\n", i, pthread_self());
        DoWork(i);
    }
}

int main(void)
{
    pthread_t child;

    pthread_create(&child, NULL, ChildProc, "Child");

    ParentProc();

    pthread_join(child, NULL); // make child a non-daemon(foreground) thread
}

现在如何将多个参数传递给 ChildProc() 方法?

一种方法是传递数组或结构。但是如果我想在没有数组或结构的情况下传递多个变量怎么办?

最佳答案

一个快速而垃圾的答案是创建一个结构来保存所有参数并传递它的指针

关于c - 如何将多个参数传递给线程函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3674104/

相关文章:

android - 在 android 上使用 FFmpeg h264 解码器

c - 倒回 VT100 终端命令中的一段文本

c - 发送消息不起作用

c++ - ASCII 艺术 - 按亮度级别对 ASCII 字符数组进行排序 (C/C++)

ubuntu - kafka 经纪人在开始时不可用

MySQL 阻塞(或者如何优化它)

java - 在android中仅从年份获取unix时间戳值

c - 如何导出打开的文件描述符以执行子级

linux - 读取时显示 "Command Not Found"的 Unix 脚本

linux - 哪些 IMS 软件包可用于 Ubuntu?