C:在不使用全局变量的情况下,从已创建的线程中操纵主线程中声明的结构
我试图理解线程在应用程序中的使用(我知道我所做的在某种意义上可能是愚蠢的),并且我试图理解如何从创建的线程操作main结构中声明的变量。到目前为止,我已经:C:在不使用全局变量的情况下,从已创建的线程中操纵主线程中声明的结构,c,linux,variables,pthreads,structure,C,Linux,Variables,Pthreads,Structure,我试图理解线程在应用程序中的使用(我知道我所做的在某种意义上可能是愚蠢的),并且我试图理解如何从创建的线程操作main结构中声明的变量。到目前为止,我已经: typedef struct Chi_Server { int thread_status; int active_connections; pthread_t thread_id; pthread_attr_t thread_attribute; struct thread_info *tinf
typedef struct Chi_Server {
int thread_status;
int active_connections;
pthread_t thread_id;
pthread_attr_t thread_attribute;
struct thread_info *tinfo;
} CHI_SERVER;
我只是把它设为=1,这样我就可以看到结构变量是否可以在主程序中进行操作。到目前为止,我完全被难住了。有人知道如何操作main中声明的结构吗?调用pthread\u create时,您似乎对引用感到困惑;您的最后一个参数“&server”可能应该是server。 您不能像在server_运行时那样取消引用指向void的指针。 您应该为void指针分配一个struct Server指针并使用它 使用
gcc-Wall-thread.c-o-thread-lpthread
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
struct Server {
pthread_attr_t thread_attribute;
pthread_t thread_id;
int active_connections;
};
void * server_runtime(void *);
/* Error checking omitted for clarity. */
int main()
{
struct Server *server;
void *result;
server = malloc(sizeof(struct Server));
pthread_attr_init(&server->thread_attribute);
pthread_create(&server->thread_id, &server->thread_attribute, server_runtime, server);
pthread_attr_destroy(&server->thread_attribute);
pthread_join(server->thread_id, &result);
printf("%d\n", server->active_connections);
free(server);
return 0;
}
void * server_runtime(void *p)
{
struct Server *server = p;
server->active_connections = 1;
return NULL;
}
#包括
#包括
#包括
结构服务器{
pthread_attr_t thread_属性;
pthread_t thread_id;
int-active_连接;
};
void*服务器运行时(void*);
/*为清楚起见,省略了错误检查*/
int main()
{
结构服务器*Server;
无效*结果;
server=malloc(sizeof(struct-server));
pthread\u attr\u init(&server->thread\u属性);
pthread\u创建(&server->thread\u id,&server->thread\u属性,server\u运行时,server);
pthread\u attr\u destroy(&server->thread\u属性);
pthread_join(服务器->线程id和结果);
printf(“%d\n”,服务器->活动\u连接);
免费(服务器);
返回0;
}
void*server\u运行时(void*p)
{
结构服务器*Server=p;
服务器->活动连接=1;
返回NULL;
}
在void*server\u运行时(void*server)。
您必须知道*服务器是什么类型的
我还没有看到您的服务器声明,但我想它看起来像这样
int main()
{
struct yourstruct server;
if (pthread_create(&server->thread_id, (void *) &server->thread_attribute,
&server_runtime, &server))
{
perror("Creating main thread");
}
}
void *server_runtime(void *_server)
{
struct yourstruct *server = _server;
server->active_connections = 1;
}
可能像往常一样错过了什么。
祝你好运。在调用pthread_create()时,它应该是
server
而不是&server
。您正在将指针传递给指向服务器的指针。@DaV将指针传递给指向服务器的指针
为什么?注意SERVER*SERVER=arg代码>我意识到了其中的错误。我已将其更改为反映如何在main()中声明server
,如果声明为likeserver*server
,则Dav是correct@GrijeshChauhan在main()
中,如果server
不是指针,则代码不会编译。调用pthread\u create()
传递&server
的类型为server**
。James C,你能告诉我你是如何声明这个结构的吗?奇怪的是,我没有为这个结构分配内存,这是导致这个问题的原因。我感谢你的帮助:)
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
struct Server {
pthread_attr_t thread_attribute;
pthread_t thread_id;
int active_connections;
};
void * server_runtime(void *);
/* Error checking omitted for clarity. */
int main()
{
struct Server *server;
void *result;
server = malloc(sizeof(struct Server));
pthread_attr_init(&server->thread_attribute);
pthread_create(&server->thread_id, &server->thread_attribute, server_runtime, server);
pthread_attr_destroy(&server->thread_attribute);
pthread_join(server->thread_id, &result);
printf("%d\n", server->active_connections);
free(server);
return 0;
}
void * server_runtime(void *p)
{
struct Server *server = p;
server->active_connections = 1;
return NULL;
}
int main()
{
struct yourstruct server;
if (pthread_create(&server->thread_id, (void *) &server->thread_attribute,
&server_runtime, &server))
{
perror("Creating main thread");
}
}
void *server_runtime(void *_server)
{
struct yourstruct *server = _server;
server->active_connections = 1;
}