Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/150.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++;-pthread未执行 我在C++中实现了多线程服务器。我的主要目标是使用线程调用一个函数,将客户机详细信息发送给其他函数。以下是我的代码片段: while(true) { if((acceptId = accept(sockId,(struct sockaddr*)&clientAddr,(socklen_t *)&address)) == -1) perror("Accept:"); inet_ntop(clientAddr.ss_family,get_ip_address((struct sockaddr *)&clientAddr),ip1, sizeof(ip1)); clientInfo *cInfo = new clientInfo; cInfo->acceptId = acceptId; strcpy(cInfo->ip, ip1); void *info = cInfo; pthread_t thread_request; pthread_create(&thread_request,NULL,&Parse::parseRequest_helper,info); // after this while loop is terminating //p.parseRequest(info); sleep(0); cout<<"\nserver: got connection from "<<ip1<<" Port:"<<clientport; // this is not reachable } while(true) { if((acceptId=accept(sockId,(struct sockaddr*)和clientAddr,(socklen_t*)和address))=-1) 佩罗尔(“接受:”); inet_ntop(clientAddr.ss_系列,get_ip_地址((struct sockaddr*)和clientAddr),ip1,sizeof(ip1)); clientInfo*cInfo=新的clientInfo; cInfo->acceptId=acceptId; strcpy(cInfo->ip,ip1); void*info=cInfo; pthread\u t thread\u请求; pthread_create(&thread_request,NULL,&Parse::parseRequest_helper,info);//之后while循环终止 //p、 请求(信息); 睡眠(0); 难道你的代码没有向我们展示你的情况的细节,但从你的评论中,我有一些观点:_C++ - Fatal编程技术网 acceptId=acceptId; strcpy(cInfo->ip,ip1); void*info=cInfo; pthread\u t thread\u请求; pthread_create(&thread_request,NULL,&Parse::parseRequest_helper,info);//之后while循环终止 //p、 请求(信息); 睡眠(0); 难道你的代码没有向我们展示你的情况的细节,但从你的评论中,我有一些观点:,c++,C++" /> acceptId=acceptId; strcpy(cInfo->ip,ip1); void*info=cInfo; pthread\u t thread\u请求; pthread_create(&thread_request,NULL,&Parse::parseRequest_helper,info);//之后while循环终止 //p、 请求(信息); 睡眠(0); 难道你的代码没有向我们展示你的情况的细节,但从你的评论中,我有一些观点:,c++,C++" />

C++;-pthread未执行 我在C++中实现了多线程服务器。我的主要目标是使用线程调用一个函数,将客户机详细信息发送给其他函数。以下是我的代码片段: while(true) { if((acceptId = accept(sockId,(struct sockaddr*)&clientAddr,(socklen_t *)&address)) == -1) perror("Accept:"); inet_ntop(clientAddr.ss_family,get_ip_address((struct sockaddr *)&clientAddr),ip1, sizeof(ip1)); clientInfo *cInfo = new clientInfo; cInfo->acceptId = acceptId; strcpy(cInfo->ip, ip1); void *info = cInfo; pthread_t thread_request; pthread_create(&thread_request,NULL,&Parse::parseRequest_helper,info); // after this while loop is terminating //p.parseRequest(info); sleep(0); cout<<"\nserver: got connection from "<<ip1<<" Port:"<<clientport; // this is not reachable } while(true) { if((acceptId=accept(sockId,(struct sockaddr*)和clientAddr,(socklen_t*)和address))=-1) 佩罗尔(“接受:”); inet_ntop(clientAddr.ss_系列,get_ip_地址((struct sockaddr*)和clientAddr),ip1,sizeof(ip1)); clientInfo*cInfo=新的clientInfo; cInfo->acceptId=acceptId; strcpy(cInfo->ip,ip1); void*info=cInfo; pthread\u t thread\u请求; pthread_create(&thread_request,NULL,&Parse::parseRequest_helper,info);//之后while循环终止 //p、 请求(信息); 睡眠(0); 难道你的代码没有向我们展示你的情况的细节,但从你的评论中,我有一些观点:

C++;-pthread未执行 我在C++中实现了多线程服务器。我的主要目标是使用线程调用一个函数,将客户机详细信息发送给其他函数。以下是我的代码片段: while(true) { if((acceptId = accept(sockId,(struct sockaddr*)&clientAddr,(socklen_t *)&address)) == -1) perror("Accept:"); inet_ntop(clientAddr.ss_family,get_ip_address((struct sockaddr *)&clientAddr),ip1, sizeof(ip1)); clientInfo *cInfo = new clientInfo; cInfo->acceptId = acceptId; strcpy(cInfo->ip, ip1); void *info = cInfo; pthread_t thread_request; pthread_create(&thread_request,NULL,&Parse::parseRequest_helper,info); // after this while loop is terminating //p.parseRequest(info); sleep(0); cout<<"\nserver: got connection from "<<ip1<<" Port:"<<clientport; // this is not reachable } while(true) { if((acceptId=accept(sockId,(struct sockaddr*)和clientAddr,(socklen_t*)和address))=-1) 佩罗尔(“接受:”); inet_ntop(clientAddr.ss_系列,get_ip_地址((struct sockaddr*)和clientAddr),ip1,sizeof(ip1)); clientInfo*cInfo=新的clientInfo; cInfo->acceptId=acceptId; strcpy(cInfo->ip,ip1); void*info=cInfo; pthread\u t thread\u请求; pthread_create(&thread_request,NULL,&Parse::parseRequest_helper,info);//之后while循环终止 //p、 请求(信息); 睡眠(0); 难道你的代码没有向我们展示你的情况的细节,但从你的评论中,我有一些观点:,c++,C++,在您所说的//在此while循环终止之后,您的生产代码是否与此处相同,或者您真的在它之后终止了您的块??还记得当您调用p.parseRequest(info)时,您正在调用线程中执行操作,因此在这一行之后您就完成了解析,但如果您使用了线程,则在这一行之后(pthread\u create(…))指定的线程可能没有事件启动,线程由操作系统安排在以后运行,您不应该认为解析是在事件启动之后完成的 在第//行中,这是不可访问的您认为该行不应该执行吗?或者这是代码在运行时的行为?在任何情况下,pthrea

在您所说的
//在此while循环终止之后
,您的生产代码是否与此处相同,或者您真的在它之后终止了您的块??还记得当您调用
p.parseRequest(info)
时,您正在调用线程中执行操作,因此在这一行之后您就完成了解析,但如果您使用了线程,则在这一行之后(
pthread\u create(…)
)指定的线程可能没有事件启动,线程由操作系统安排在以后运行,您不应该认为解析是在事件启动之后完成的

在第
//行中,这是不可访问的
您认为该行不应该执行吗?或者这是代码在运行时的行为?在任何情况下,
pthread\u create
立即返回,稍后在另一个执行线程中运行作业,因此在您的代码中几乎立即调用
sleep(0)
然后确保到达指定行

sleep(0)
的原因是什么?它是为了让线程启动还是完成?在任何情况下,操作系统都会根据内部算法安排线程,因此在
sleep(0)
之后,线程执行可能仍然挂起!!如果要确保线程启动/停止,必须使用同步机制(例如互斥锁)在线程和主线程之间查看代码:

static void * Parse::parseRequest_helper(void *c)    
{
     cout<<"Inside Helper"; // Not coming here
     return ((Parse *)c)->parseRequest(c);
}
只有当传递给此函数的
info
参数是指向
clientInfo
的指针时,这才有意义

因为它是同一个参数,所以这段代码毫无意义。它可以是指向
解析的指针,也可以是指向
clientInfo
的指针,但不能两者都是

     cout<<"Inside Helper"; // Not coming here

cout请重新格式化代码,在代码前添加4个空格,使其成为代码块。否则它看起来会很奇怪。如果我需要从函数(我在线程中使用的函数)返回NULL,您可能需要检查pthread_create()的返回值.?是的,您应该这样做,但这与您的问题无关。我的建议是检查pthread_create()是否正确创建了线程。顺便说一句,您应该在while(true)中添加空格,使其成为代码块:)这只是一个输入错误:在创建线程时,您正在使用&Parse::parseRequest\u helper,但您的函数仅定义为parseRequest\u helper-这是类内部定义的静态方法吗?此外,关键字是“static”,而不是“static”-这是一个输入错误吗?睡眠后是(0)和cout语句我正在结束while循环。我已经尝试给sleep一些参数,但这不起作用。所以我猜操作系统正在安排我的线程稍后执行…那么我应该使用互斥体吗?如果你有一些依赖于你的解析完成的东西(以及你的线程的结果)然后是的,你应该使用互斥体!!我还有一个疑问,我正在使用这个线程来收集和存储队列中的客户端信息。但同时我需要我的main返回并接受新的请求,并再次将新的客户端数据提供给这个线程。是否可以使用互斥体…?如果我使用pthread\u join approa呢ch..?据我所知,您在main中收到一个请求,并将其传递给一个新线程来处理它,是否正确?如果是,则不需要等待主线程中的线程完成。线程应该完成其操作,例如,将结果发送给客户端,并可能关闭连接。main应该继续并接受新的客户端请求,就这些!!所以你不需要等待任何事情,只需要同步对多个线程将访问的全局资源的访问。是的,线程将处理请求,main将再次接受新连接…那么我应该如何继续..互斥或pthread_join..因为我不知道这是什么时候s线程将由操作系统执行..我是线程新手?那么我应该如何继续?我理解你的观点,但是我应该如何通过线程将结构发送到parseRequest\u帮助函数并从那里发送到parseRequest函数…有什么建议吗..非常感谢你的建议..我按照你的建议一步一步地进行,并且成功了..谢谢你的帮助ng everyone…:)一个常见的模式是:1)定义一个包含所有需要传递给线程的参数的结构。2)分配一个该类型的新结构并填充它。3)将指向新结构的指针传递给线程。4)当线程处理完该结构后,它应该释放它。(除非它包含线程需要返回给调用它的代码的信息,在这种情况下,它可以被引用计数,或者其他一些代码可以在线程使用它之后删除它。)
void * Parse::parseRequest(void *info)
{

    cout<<"Inside Parse Request";     //Not coming here
    clientInfo *cInfo = (struct clientInfo *)info;
        cout<<cInfo->ip;
}
     cout<<"Inside Helper"; // Not coming here