传递给线程更改的参数

Parameter passed to pthread changes

本文关键字:参数 线程      更新时间:2023-10-16
我不知道

为什么传递给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: %xn", 
           params);
   printf("->socket: %xn", 
           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 elsen");
       fflush(stdout);
       }
       if(!answ){
       printf("finerichiestan");
       fflush(stdout);
       fineRichiesta(serverinfo);
       }
       printf("answersw: %sn", 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 richiestan");
       fflush(stdout);
   serverinformation->server->disconnect(serverinformation->serverConn);
   free(serverinformation);
   pthread_exit(NULL);
}

任何帮助都会很棒,我疯了 -我不知道我的问题是否最好表述——

提前感谢...

您的pthread_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