[英]Passing argc and argv to a thread in c
Im trying to code a chat in C with a graphical interface. 我试图用图形界面在C语言中编写聊天记录。 On one side, I have a client for my chat, on the other side I have a GTK app to display my chat. 一方面,我有一个聊天的客户端,另一方面,我有一个GTK应用程序来显示我的聊天。 To run them both at the same time, I'll be using pthread (open to other suggestions tho). 要同时运行它们,我将使用pthread(欢迎使用其他建议。)
My problem is that I need both argc and argv in order for my client to work properly. 我的问题是我需要argc和argv才能使客户端正常工作。 From what I saw, pthread_create() takes as argument a function to execute and only one argument. 从我看到的情况来看,pthread_create()将要执行的函数作为参数,并且仅接受一个参数。 I saw from other topics that I could use a data structure to contain them both, but I can't seem to cast them properly. 从其他主题中可以看出,我可以使用数据结构来包含它们两者,但是似乎无法正确地将它们强制转换。 Here is what I have : 这是我所拥有的:
typedef struct {
int *argc;
char ** argv;
} args_list;
void * simple_client(void * arg){
int argc = (int) arg->argc;
char * argv[] = (char *) arg->argv;
printf("in thread : %d / %s, %s \n", argc, argv[0], argv[1]);
return 0;
}
int main(int argc, char * argv[])
{
printf("int main : %d / %s, %s \n", argc, argv[0], argv[1]);
args_list * args = malloc(sizeof *args);
args-> argc = &argc;
args-> argv = &argv;
pthread_t thread1;
if (pthread_create(&thread1, NULL, simple_client, args)) {
perror("pthread_create");
return EXIT_FAILURE;
}
if (pthread_join(thread1, NULL)) {
perror("pthread_join");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
From this, I get : error: request for member 'argc' in something not a structure or union from int argc = (int) arg->argc; 由此,我得到了:错误:从int argc =(int)arg-> argc;请求的不是结构或联合的成员'argc'; and the same with argv from char * argv[] = (char *) arg->argv; 和来自char * argv [] =(char *)arg-> argv的argv相同;
What would be causing these errors ? 是什么导致这些错误? And do you think this is a good way of implementating an online chat with a GTK gui ? 您认为这是与GTK gui进行在线聊天的好方法吗?
Thanks in advance 提前致谢
Define the struct
as so: 如此定义struct
:
typedef struct tARGS
{
int argc;
char **argv;
} ARGS;
Then, in main
: 然后,在main
:
ARGS args;
args.argc = argc;
args.argv = argv;
Technically, argc
isn't really needed since the following code will process every argument in args
( argv[argc]
is a guaranteed null pointer by the C standard): 从技术上讲,实际上并不需要argc
,因为以下代码将处理args
中的每个参数(根据C标准, argv[argc]
是保证为空的指针):
ARGS args = *(ARGS *)arg;
while(*args.argv)
{
puts(*args.argv);
args.argv++;
}
A few issues with types here. 这里的一些类型问题。
simple_client
takes a void*
. simple_client
取一个void*
。 In order to use the passed in value as an arg_list
, you need to cast it to one. 为了将传入的值用作arg_list
,您需要将其arg_list
为1。
void * simple_client(void * argp){
arg_list* arg = (arg_list*) argp;
You don't need an extra level of indirection in arg_list
. 您不需要arg_list
的额外间接arg_list
。 It should contain the same types as the arguments of main. 它应包含与main参数相同的类型。
//in main:
args->argc = argc;
args->argv = argv;
//in simple_client:
int argc = arg->argc;
char** argv = arg->argv;
In your original code you were storing the "address of a pointer to a char*" in a "pointer to a char *". 在原始代码中,您将“指向char *的指针的地址”存储在“指向char *的指针”中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.