传递给线程更改的参数
Parameter passed to pthread changes
我不知道
为什么传递给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
相关文章:
- C++线程中,没有重载函数接受 X 参数
- C++ 中的线程不能使用参数
- 使用带有 ref 参数的成员函数创建线程时出现编译错误
- C++线程找不到函数作为参数(链接器)
- 如何将带有参数的方法传递给线程以执行?
- 为什么 beginthreadex 线程参数变量在父线程中没有更新
- 如何将参数传递给已经开始运行的线程
- 线程构造函数周围的可变参数模板包装器无法编译
- 线程函数中的c++参数更改
- 参数包推导不一致 int 和 int& 在可变参数模板化成员函数中创建运行成员函数的线程
- 线程函数参数的前向声明不起作用
- 带参数的线程调用库函数
- 错误:静态断言失败:std ::线程参数必须在转换为rvalues后不可行
- 从从可调用参数创建的线程对象参数移动构造 C++11 线程
- 错误 C2064:term 的计算结果不是采用 1 个参数的函数 - 关于线程的一些东西
- 通过将两个参数包的元素线程化为类型对来解压缩这两个参数包
- 提升线程:术语的计算结果不为 0 个参数
- C++线程错误 - 带有类参数的编译时错误
- 在 C++11 中,如何不将参数传递给线程
- 在单独的线程中使用参数启动成员函数