简体   繁体   English

c pthread_create字符串参数

[英]c pthread_create string argument

I using c thread. 我用c线程。

I want to using string argument. 我想使用字符串参数。

in my source 在我的来源

int main(int argc, char **argv){
  pthread_t thread[1];
  pthread_create(&thread[0], NULL, thread_A, (void *) argv[0]);
  pthread_join(thread[0], NULL)
}

void * thread_A(void * arg){
  char argv[100] = {0};
  strcpy(argv, (char *) arg);
}

when comfile this warning like this 当comfile这样的警告时

pthread.h:225:12: note: expected 'void * (*)(void *)' but argument is of type 'void * (*)(char **)' pthread.h:225:12:注意:预期'void *(*)(void *)'但参数的类型为'void *(*)(char **)'

so I use (void *)&argv[0]. 所以我使用(void *)&argv [0]。 but this is error too. 但这也是错误的。

what can I try? 我该怎么办?

Doing

pthread_create(&thread[0], NULL, thread_A, (void *) argv[0]);

and even without the cast 甚至没有演员

pthread_create(&thread[0], NULL, thread_A, argv[0]);

would be perfectly fine if you want to pass a string as an argument. 如果你想传递一个字符串作为参数,那就完全没问题。 But the warning you are seeing 但是你看到的警告

pthread.h:225:12: note: expected void * (*)(void *) but argument is of type void * (*)(char **) pthread.h:225:12:注意:期望void * (*)(void *)但参数类型为void * (*)(char **)

is not talking about the forth parameter (the argument to the thread function), it's about the third parameter: the function pointer. 不是在讨论第四个参数(线程函数的参数),它是关于第三个参数:函数指针。 In this case is says that the function you are passing has this signature: 在这种情况下,表示您传递的函数具有以下签名:

void *thread_A(char **arg);

when it's expecting a 当它期待一个

void *thread_A(void *arg);

The code you posted doesn't generate the warning you've posted and I assume that the code you are really using declares thread_A as a void * (*)(char **) and not as void * (*)(void*) . 您发布的代码不会生成您发布的警告,我认为您实际使用的代码将thread_A声明为void * (*)(char **)而不是void * (*)(void*) So check you code carefully. 所以仔细检查你的代码。

So the real cast you are looking for would be 所以你正在寻找真正的演员阵容

pthread_create(&thread[0], NULL, (void * (*)(void*)) thread_A, argv[0]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM