传递给pthread的参数更改

时间:2019-03-29 16:06:18

标签: 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= 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 *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 fineRichiesta(ServerInfo * serverinformation){
   printf("im in fine richiesta\n");
       fflush(stdout);
   serverinformation->server->disconnect(serverinformation->serverConn);
   free(serverinformation);
   pthread_exit(NULL);
}

任何帮助都将是惊人的,我快疯了- 我不知道我的问题是否是最好的表述方式-

提前感谢...

1 个答案:

答案 0 :(得分:2)

您的pthread_create调用正在将错误的void *值传递给线程启动例程manageConnection

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

pthread_create(&manageConnection_thread, 
               NULL,
               manageConnection, 
               (void*) &serverinfo)

应该 传递serverInfo变量的 value ,该变量指向已填充的ServerInfo

pthread_create(&manageConnection_thread, 
               NULL,
               manageConnection, 
               (void*) serverinfo)  // <-- note: ampersand removed here