首页 \ 问答 \ 线程工作者和epoll(Thread workers and epoll)

线程工作者和epoll(Thread workers and epoll)

我正在尝试实现一个处理许多tcp连接的服务器,每天100-1000个连接,基于一天的时间。 在阅读了很多关于每个连接的线程的c10k问题并且仅使用epoll之后,我决定使用它们作为线程池并且main将充当调度程序,因此每个新连接将被分配给线程。

我有很多问题在其他任何地方找不到答案。 以下线程安全吗? 在添加新的fd之前我需要锁定吗?

int main ()
{
    while(i < number_threads)
      {         
        pthread_create( &id , NULL ,  worker , (void*) epoll_fd[i]);
        i++;
      }


//is it ok to add the new_sock for the epoll_fd[i] so the thread can pick it up
int y = 0;
    while(1) {
        new_sock = accept(...);
           if (epoll_ctl(epoll_fd[y], EPOLL_CTL_ADD, new_sock, &ev) < 0)
            {
                print error; 
            }
    y++;
    if (y == number_threads)
    y = 0;
    }

}



void *worker(void *epfd)
{
epoll_wait //start waiting for event
}

I'm trying to implement a server that deals with many tcp connections, from 100 - 1000 connections/day based on the time of the day. after reading a lot about the c10k problem with thread per connection and using epoll only I decided to use both as a pool of threads and the main will act as a dispatcher, so each new connection will be assigned to a thread.

I have a number of questions that I can't find answers anywhere else. Is the following thread safe? Do I need to lock before adding the new fd?

int main ()
{
    while(i < number_threads)
      {         
        pthread_create( &id , NULL ,  worker , (void*) epoll_fd[i]);
        i++;
      }


//is it ok to add the new_sock for the epoll_fd[i] so the thread can pick it up
int y = 0;
    while(1) {
        new_sock = accept(...);
           if (epoll_ctl(epoll_fd[y], EPOLL_CTL_ADD, new_sock, &ev) < 0)
            {
                print error; 
            }
    y++;
    if (y == number_threads)
    y = 0;
    }

}



void *worker(void *epfd)
{
epoll_wait //start waiting for event
}

原文:https://stackoverflow.com/questions/15626234
更新时间:2024-04-09 12:04

最满意答案

直接使用SimpleXML的元素访问和属性访问,而不是使用XPath查询将使代码更简单,性能更好。 你的代码可以减少到...

$xml_report_abbrev_b = $xml->xpath('//poster[@name="U-Verify"]');

if($xml_report_abbrev_b){
    echo '<h1>'.$xml_report_abbrev_b[0]->full_image['url'].'</h1>';
} else {
    echo 'XPath query failed';
}

请注意echo行的方式 - 使用您从XPath表达式中找到的<poster>元素,使用<full_image>元素并获取url属性。

我也将[0]移到if因为如果XPath找不到值,则会产生错误,因为没有任何数据可以从中获取值。

这输出...

<h1>u-verify.jpg</h1>

Using SimpleXML's element access and attribute access directly instead of using the XPath query will make the code simpler and perform better. Your code could be reduced to...

$xml_report_abbrev_b = $xml->xpath('//poster[@name="U-Verify"]');

if($xml_report_abbrev_b){
    echo '<h1>'.$xml_report_abbrev_b[0]->full_image['url'].'</h1>';
} else {
    echo 'XPath query failed';
}

Note the way the echo line says - with the <poster> element you found from the XPath expression, use the <full_image> element and fetch the url attribute.

I also moved the [0] into the if because if the XPath didn't find a value, this produced an error as there isn't any data to get a value from.

This outputs...

<h1>u-verify.jpg</h1>

相关问答

更多
  • 要将它作为字符串,只需通过(string)强制转换它,它将在内部调用SimpleXMLElement的__toString()以返回字符串表示。 顺便说一下,当你echo它时,也会发生这种情况。 while (list(, $node) = each($result)) { $feeddata [] = (string)$node[0]; } To get it as a string, simply cast it via (string), which will internally call ...
  • 从一点在线阅读,这不限于任何特定的PHP或其他库,但XPath本身 - 至少在XPath版本1.0 XPath 1.0不包含任何“默认”命名空间的概念,因此无论XML源中如何显示元素名称,如果它们具有绑定到它们的名称空间,则它们的选择器必须以表单的基本XPath选择器作为前缀ns:name 。 请注意, ns是XPath处理器中定义的前缀,而不是正在处理的文档,因此与XML表示中xmlns属性的使用方式没有关系。 参见例如这个“常见的XSLT错误”页面 ,谈论密切相关的XSLT 1.0: 要访问XPath中 ...
  • DOMXpath::evaluate()的第二个参数是表达式的上下文。 这是一个处理xml的小例子: $document = new DOMDocument(); $document->loadXml($xml); $xpath = new DOMXpath($document); foreach ($xpath->evaluate('//mother') as $mother) { var_dump( $xpath->evaluate('count(daughter)', $mother), ...
  • 直接使用SimpleXML的元素访问和属性访问,而不是使用XPath查询将使代码更简单,性能更好。 你的代码可以减少到... $xml_report_abbrev_b = $xml->xpath('//poster[@name="U-Verify"]'); if($xml_report_abbrev_b){ echo '

    '.$xml_report_abbrev_b[0]->full_image['url'].'

    '; } else { echo 'XPath query f ...
  • 事情在先。 要删除local-name()并停止忽略名称空间,请为其注册一个前缀。 之后,这只是一个简单的条件。 $worksheet = new SimpleXMLElement($xml); $worksheet->registerXpathNamespace( 'm', 'http://schemas.openxmlformats.org/spreadsheetml/2006/main' ); var_dump( $worksheet->xpath('//m:row[@r=18]') ); ...
  • PHP的SimpleXML仅适用于返回节点集的查询。 count(...)返回不支持的标量值。 使用更强大的DOMXPath或计算结果数组中的对象: var_dump(count($s->xpath("node"))); INT(2) PHP's SimpleXML only works on queries which return nodesets. count(...) returns a scalar value which is not supported. Use DOMXPath which ...
  • 你需要的是XPath ,更具体地说是SimpleXML的xpath()方法 。 您可以使用XPath的/运算符遍历,而不是遍历使用PHP的->运算符,否则,可以实现您想要的效果: $comp = 'component[1]/structuredBody[1]/component'; echo count( $xml->xpath($comp) ); 您可能认为这可以简化为'component/structuredBody/component' ,但是会找到与表达式匹配的所有可能路径 - 即如果有多个str ...
  • 我不认为你可以把它放到那种数组中(你需要告诉PHP放置哪些标签,子节点,属性等),但是你可以使用DOMXPath类获取DOMNode元素,这会给你一个DOMNodeList对象: $document = new DOMDocument(); $document->load($myXmlFile); $xpath = new DOMXPath($document); $result = $xpath->query('//node()[@page="1"]'); var_dump($result->lengt ...
  • 我冒昧地说,这段代码肯定不符合你的要求: $newVerse = simplexml_load_string($strippedresponse); $oldVerse = $xmlDoc->xpath("//div[@number='".$div1."']/div[@number='".$div2."']"); $oldVerse = $newVerse; 更改PHP变量的值没有副作用。 换句话说,当你做$a = $b;时没有任何反应$a = $b; 除了在某些特定情况下,它不是其中之一。 我不知道你 ...
  • 我认为你不能将$xml传递给XSLTProcessor::transformToXML方法,因为它的数组(由SimpleXMLElement::xpath ): PHP警告:XSLTProcessor :: transformToXml()期望参数1是对象,第11行的/var/www/index.php中给出的数组 简单的补救措施是将XPath表达式放入XSL样式表中:

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。