C++ 传递给pthread changes的参数

C++ 传递给pthread changes的参数,c++,c,C++,C,我不知道为什么传递给pthread的参数会自动更改。 我会给你看我的代码 调试结果如下: begin of while while() =begin= 0 === params: a84a5310 ->socket: a84a4e70 ->connection: a84a52d0(4) ->webserver: a84a4ea0(0) ===== begin of while while() =end==== ===== end of while() =begin= 1709

我不知道为什么传递给pthread的参数会自动更改。 我会给你看我的代码

调试结果如下:

begin of while while() =begin= 0 ===
params: a84a5310
->socket: a84a4e70
->connection: a84a52d0(4)
->webserver: a84a4ea0(0)
===== begin of while while() =end====

===== end of while() =begin= 1709692672 ===
params: a84a5310
->socket: a84a4e70
->connection: a84a52d0(4)
->webserver: a84a4ea0(0)
===== end of while() =end====
Accepting...

===== in thread() =begin= 1709692672 ===
params: aacf4240//wrong expected a84a5310
->socket: a84a5310 //wrong expected a84a4e70
->connection: a84a4ea0(0)//wrong expected a84a52d0(4)
->webserver: 65e7d700(0) //wrong expected a84a4ea0(0)
===== in thread() =end====

这是我传递给线程的结构:

typedef struct  InfoServer{
   ServerTCP* server;
   WebServer* webserver;
   ConnServer* serverConn;
}ServerInfo;
这是调试功能:

void* show_params(ServerInfo* params, char* where, int n) {

   printf("===== %s =begin= %d ===\n",where, n); 

   printf("params: %x\n", 
           params);
   printf("->socket: %x\n", 
           params->server);
   printf("->connection: %x(%d)\n", 
           params->serverConn, params->serverConn->getConn());
   printf("->webserver: %x(%d)\n", 
           params->webserver);
   printf("===== %s =end====\n",where); 

   fflush(stdout);

}
这是主要功能:

void *manageConnection(void *serverConn);
void fineRichiesta(ServerInfo * serverinformation);

int main (int argc , char* argv[]){
   if(argc!=2){
       printf("USAGE: %S , PORT  \n",argv[0]);
   fflush(stdout);
       exit(1);
   }

   int port = atoi(argv[1]);

   ServerTCP* tserver= new ServerTCP(port);
   WebServer* webserv= new WebServer();


   bool exit = false;
   while( true ){

       pthread_t manageConnection_thread;
       ConnServer* serverC=  tserver->acceptConn();
       ServerInfo* serverinfo = (ServerInfo*) malloc(sizeof(ServerInfo));
       serverinfo->server = tserver;
       serverinfo->webserver = webserv;
       serverinfo->serverConn =serverC;


       show_params(serverinfo,"starting in while()",0);

       if(pthread_create(&manageConnection_thread, 
               NULL,
               manageConnection, 
               (void*) &serverinfo)) {
                           error("Error Creating server");
           }
       show_params(serverinfo,"end of while()", manageConnection_thread);

   }
   delete( tserver);
   delete( webserv);
   return 0;
}
void fineRichiesta(ServerInfo * serverinformation){
   printf("im in fine richiesta\n");
       fflush(stdout);
   serverinformation->server->disconnect(serverinformation->serverConn);
   free(serverinformation);
   pthread_exit(NULL);
}
以下是线程代码:

void *manageConnection(void * serverinformation){
       ServerInfo *serverinfo = (ServerInfo*)serverinformation;
   show_params(serverinfo,"in thread()", pthread_self());

       ConnServer * srvConn = serverinfo->serverConn;
       char* answ;
       if(srvConn){
            answ = (char*)srvConn->riceviServer();
       }

       else    {
       printf("im in else\n");
       fflush(stdout);
       }

       if(!answ){
       printf("finerichiesta\n");
       fflush(stdout);

       fineRichiesta(serverinfo);
       }
       printf("answ: %s\n", answ);
   fflush(stdout);
       char* file = serverinfo->webserver->getFile(answ);
       srvConn->inviaServer(file);     
       fineRichiesta( serverinfo);
       free(answ);
   return NULL;
}
功能:

void *manageConnection(void *serverConn);
void fineRichiesta(ServerInfo * serverinformation);

int main (int argc , char* argv[]){
   if(argc!=2){
       printf("USAGE: %S , PORT  \n",argv[0]);
   fflush(stdout);
       exit(1);
   }

   int port = atoi(argv[1]);

   ServerTCP* tserver= new ServerTCP(port);
   WebServer* webserv= new WebServer();


   bool exit = false;
   while( true ){

       pthread_t manageConnection_thread;
       ConnServer* serverC=  tserver->acceptConn();
       ServerInfo* serverinfo = (ServerInfo*) malloc(sizeof(ServerInfo));
       serverinfo->server = tserver;
       serverinfo->webserver = webserv;
       serverinfo->serverConn =serverC;


       show_params(serverinfo,"starting in while()",0);

       if(pthread_create(&manageConnection_thread, 
               NULL,
               manageConnection, 
               (void*) &serverinfo)) {
                           error("Error Creating server");
           }
       show_params(serverinfo,"end of while()", manageConnection_thread);

   }
   delete( tserver);
   delete( webserv);
   return 0;
}
void fineRichiesta(ServerInfo * serverinformation){
   printf("im in fine richiesta\n");
       fflush(stdout);
   serverinformation->server->disconnect(serverinformation->serverConn);
   free(serverinformation);
   pthread_exit(NULL);
}
任何帮助都会令人惊讶,我快疯了- 我不知道我的问题是否表达得最好-


提前感谢…

您的
pthread\u create
调用将错误的
void*
值传递给线程启动例程
manageConnection

它正在传递
serverInfo
变量的地址:

pthread_create(&manageConnection_thread, 
               NULL,
               manageConnection, 
               (void*) &serverinfo)
它应该传递
serverInfo
变量的值,该变量指向已填充的
serverInfo

pthread_create(&manageConnection_thread, 
               NULL,
               manageConnection, 
               (void*) serverinfo)  // <-- note: ampersand removed here
pthread\u创建(&manageConnection\u线程),
无效的
管理连接,

(void*)serverinfo)//您的
pthread\u create
调用将错误的
void*
值传递给线程启动例程
manageConnection

它正在传递
serverInfo
变量的地址:

pthread_create(&manageConnection_thread, 
               NULL,
               manageConnection, 
               (void*) &serverinfo)
它应该传递
serverInfo
变量的值,该变量指向已填充的
serverInfo

pthread_create(&manageConnection_thread, 
               NULL,
               manageConnection, 
               (void*) serverinfo)  // <-- note: ampersand removed here
pthread\u创建(&manageConnection\u线程),
无效的
管理连接,

(void*)serverinfo)//将
(void*)和serverinfo更改为
(void*)serverinfo
。您希望将指向已分配的
ServerInfo
的指针传递给线程,而不是指向
ServerInfo
变量的指针@IanAbbott OMG,它正在工作,非常感谢你。。。损失了4个小时,我怎么接受你的回答?将
(void*)和serverInfo
更改为
(void*)serverInfo
。您希望将指向已分配的
ServerInfo
的指针传递给线程,而不是指向
ServerInfo
变量的指针@IanAbbott OMG,它正在工作,非常感谢你。。。损失了4个小时,我怎么接受你的回答?