如何将 char* argv[] 传递给 pthread_create?
我试图将传递到主线程的任何参数传递给我用“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
main
中的argv
是char**
,而不是char*
,所以这就是您应该转换的内容它回到threadMainLoop
中。The
argv
inmain
is achar**
, not achar*
, and so that's what you should cast it back to inthreadMainLoop
.现在可以了……感谢史蒂夫在写作方向上的推动……
This works now...thanks Steve for the push in the write direction.....