如何将 char* argv[] 传递给 pthread_create?

发布于 2024-11-04 02:52:55 字数 609 浏览 1 评论 0原文

我试图将传递到主线程的任何参数传递给我用“pthread_create”创建的“子线程”。

void *threadMainLoop(void *arg){
    char *arguments = (char*)arg;
    printf("arg 1 - %s\n", arguments[1]);

}

int main(int argc, char *argv[]){
    printf("Start of program execution\n");

    rc = pthread_create(&outboundThread, NULL, threadMainLoop, (void *) argv);
    printf("Thread create rc: %i, %d\n", rc, outboundThread);
    if(rc != 0){
        printf("Thread creation failed\n");
        exit(1);
    }
    pthread_join(outboundThread, NULL);
    return 0;
}

上面的代码不起作用,你能告诉我如何在线程中访问 ARGV 数组,如“argv[0]”等吗?

I am trying to pass whatever arguments are passed into the MAIN thread to a "sub thread" I create with "pthread_create".

void *threadMainLoop(void *arg){
    char *arguments = (char*)arg;
    printf("arg 1 - %s\n", arguments[1]);

}

int main(int argc, char *argv[]){
    printf("Start of program execution\n");

    rc = pthread_create(&outboundThread, NULL, threadMainLoop, (void *) argv);
    printf("Thread create rc: %i, %d\n", rc, outboundThread);
    if(rc != 0){
        printf("Thread creation failed\n");
        exit(1);
    }
    pthread_join(outboundThread, NULL);
    return 0;
}

The above code does not work, can you please show me how I can access the ARGV array like "argv[0]" etc in the thread?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

平安喜乐 2024-11-11 02:52:55

main 中的 argvchar**,而不是 char*,所以这就是您应该转换的内容它回到threadMainLoop中。

The argv in main is a char**, not a char*, and so that's what you should cast it back to in threadMainLoop.

潦草背影 2024-11-11 02:52:55

现在可以了……感谢史蒂夫在写作方向上的推动……

void *threadMainLoop(void *arg){
    char **arguments = (char**)arg;   
    printf("args[0] =%s\n", arguments[0]);
    printf("args[1] =%s\n", arguments[1]);
}

int main(int argc, char *argv[]){
    printf("Start of program execution\n");

    rc = pthread_create(&outboundThread, NULL, threadMainLoop, (void *) argv);
    printf("Thread create rc: %i, %d\n", rc, outboundThread);
    if(rc != 0){
        printf("Thread creation failed\n");
        exit(1);
    }
    pthread_join(outboundThread, NULL);
    return 0;
}

This works now...thanks Steve for the push in the write direction.....

void *threadMainLoop(void *arg){
    char **arguments = (char**)arg;   
    printf("args[0] =%s\n", arguments[0]);
    printf("args[1] =%s\n", arguments[1]);
}

int main(int argc, char *argv[]){
    printf("Start of program execution\n");

    rc = pthread_create(&outboundThread, NULL, threadMainLoop, (void *) argv);
    printf("Thread create rc: %i, %d\n", rc, outboundThread);
    if(rc != 0){
        printf("Thread creation failed\n");
        exit(1);
    }
    pthread_join(outboundThread, NULL);
    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文