问题列表 - 第36009页

在多线程环境中从STL Map读取/写入

问题:我需要编写一个函数,该函数从地图返回输入键的值.如果函数找不到map中的值,它将从数据库中获取值,写入map以供将来使用并返回相同的值.可以有多个线程调用此函数.

我正在考虑这条线:

string GetData (const int key)
{

    pthread_rwlock_rdlock(&rwlock); //read lock
    string result = "not found";
    my_map::const_iterator iter = m.find(key);
    if ( iter != m.end() )//found
    {
       result = iter->second;
    }
    else //missing
    {
        pthread_rwlock_wrlock(&rwlock); // write lock
        //fetch value from data base


        //if successful, add to the map
          m[key] = "missing data";
          result = "missing data";
     pthread_rwlock_unlock(&rwlock); // unlock write lock
    }
    pthread_rwlock_unlock(&rwlock); // unlock read lock
    return result;
}
Run Code Online (Sandbox Code Playgroud)

这个功能线程安全吗?两个或多个线程是否有可能在写锁定时排队并从数据库中查询相同的密钥?如果是,我该如何避免这种情况?

c++ multithreading stl pthreads

1
推荐指数
1
解决办法
3838
查看次数

YUI Compressor Maven插件不压缩JS文件

我正在使用YUI Compressor压缩我的Web应用程序中的JS文件.

我已经按照插件的网站说明配置了插件.

这是POM插件conf:

   <plugin>
    <groupId>net.sf.alchim</groupId>
    <artifactId>yuicompressor-maven-plugin</artifactId>
    <version>0.7.1</version>  
    <executions>
      <execution>
     <phase>compile</phase>
        <goals>
         <goal>jslint</goal>
          <goal>compress</goal>
        </goals>
      </execution>
    </executions>        
    <configuration>
    <failOnWarning>true</failOnWarning>
      <nosuffix>true</nosuffix>
      <force>true</force>
      <aggregations>
       <aggregation>
          <!-- remove files after aggregation (default: false) -->
          <removeIncluded>false</removeIncluded>
          <!-- insert new line after each concatenation (default: false) -->
          <insertNewLine>false</insertNewLine>
          <output>${project.basedir}/${webcontent.dir}/js/compressedAll.js</output>
          <!-- files to include, path relative to output's directory or absolute path-->
          <!--inputDir>base directory for non absolute includes, default to parent dir of output</inputDir-->
          <includes>                
            <include>**/autocomplete.js</include>
            <include>**/calendar.js</include>
            <include>**/dialogs.js</include>
            <include>**/download.js</include>
            <include>**/folding.js</include>
            <include>**/jquery-1.4.2.min.js</include>
            <include>**/jquery.bgiframe.min.js</include> …
Run Code Online (Sandbox Code Playgroud)

maven-2 yui-compressor

6
推荐指数
2
解决办法
9849
查看次数

如何增加列表视图的视图大小?

这是我使用的xml代码

但即使尝试增加最小高度也没用

请帮忙

替代文字

  <?xml version="1.0" encoding="utf-8"?>
<ScrollView
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
    <!-- Here you put the rest of your current view-->

<LinearLayout 
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"

  >
    <TextView  
    android:text="ID:"
    android:textSize="15dp"
    android:textStyle="bold"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
   />   

   <TextView
    android:id="@+id/cid"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
   />

   <TextView  
    android:text="Name:"
    android:textSize="15dp"
    android:textStyle="bold"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
   />

   <TextView
    android:id="@+id/con"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
   />

   <TextView  
    android:text="Posted By:"
    android:textSize="15dp"
    android:textStyle="bold"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
   />

   <TextView
    android:id="@+id/poby"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
   />

   <TextView  
    android:text="Posted Time:"
    android:textSize="15dp"
    android:textStyle="bold"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
   />

  <TextView
   android:id="@+id/poti"
   android:layout_width="fill_parent" 
   android:layout_height="wrap_content" 
   />


    <TextView  
    android:text="Annotation ID:"
    android:textSize="15dp"
    android:textStyle="bold" …
Run Code Online (Sandbox Code Playgroud)

android listview

5
推荐指数
1
解决办法
3383
查看次数

更改运行adb服务器的默认端口(即5037)

我是一个崭露头角的Android开发人员,如果没有简单的方法来配置adb服务器在另一个端口上运行那么工具的不灵活性将迫使我退出Android应用程序开发.

网络搜索没有返回任何解决方案.

我还在android sdk目录中的所有文件中搜索了"5037",但没有在那里找到设置.

port android default adb

10
推荐指数
3
解决办法
4万
查看次数

标准C功能:检查-1或0?

许多标准C和POSIX函数返回-1的错误,并0成功,例如truncate,fflush,msync等.

int ret = truncate("/some/file", 42);
Run Code Online (Sandbox Code Playgroud)

ret != -1或检查成功是否更好ret == 0?为什么?

我的想法

这是我的经验,大多数人检查错误情况(ret != -1),因为通常只有一个(考虑返回NULLEOF出错的函数).然而事后看来,人们可以看到这些功能可以从errno直接返回中受益(在哪里0被认为没有错误).

还担心函数会返回除0or -1之外的其他内容,或者稍后会添加其他返回值.在这些场景中,测试表示成功(ret == 0)的"最紧密"值的值是有意义的.

Update0

人们都知道这EOF是我的一个假设,通常被定义为-1.

c c++ correctness conventions return-value

5
推荐指数
2
解决办法
427
查看次数

降低Task.Factory.StartNew线程的优先级

像下面这样的代码将启动一个新线程来完成这项工作.有什么方法可以控制该线程的优先级吗?

Task.Factory.StartNew(() => {
    // everything here will be executed in a new thread.
    // I want to set the priority of this thread to BelowNormal
});
Run Code Online (Sandbox Code Playgroud)

.net c# multithreading .net-4.0 taskfactory

35
推荐指数
4
解决办法
4万
查看次数

使用海葵和nokogiri进行屏幕刮擦需要帮助

我有一个http://www.example.com/startpage的起始页面,其中有1220个列表以标准方式分页,例如每页20个结果.

我有代码工作,解析结果的第一页,并在其网址中包含"example_guide/paris_shops"的链接.然后我使用Nokogiri来提取该最终页面的特定数据.一切正常,20个结果写入文件.

但是,我似乎无法弄清楚如何让Anemone爬到结果的下一页(http://www.example.com/startpage?page=2),然后继续解析该页面,然后再解析第3页页面(http://www.example.com/startpage?page=3)等.

所以我想问一下是否有人知道如何在页面上启动海葵,解析该页面上的所有链接(以及特定数据的下一级数据),然后按照分页到下一页的结果所以海葵可以再次开始解析,等等.鉴于分页链接与结果中的链接不同,Anemone当然不会遵循它们.

目前我正在加载第一页结果的网址,让它完成,然后粘贴到第二页结果等的下一个网址等.非常手动和低效,特别是对于获取数百页.

任何帮助将非常感激.

require 'rubygems'
require 'anemone'
require 'nokogiri'
require 'open-uri'

Anemone.crawl("http://www.example.com/startpage", :delay => 3) do |anemone|
  anemone.on_pages_like(/example_guide\/paris_shops\/[^?]*$/) do | page |

doc = Nokogiri::HTML(open(page.url))

name = doc.at_css("#top h2").text unless doc.at_css("#top h2").nil?
address = doc.at_css(".info tr:nth-child(3) td").text unless doc.at_css(".info tr:nth-child(3) td").nil?
website = doc.at_css("tr:nth-child(5) a").text unless doc.at_css("tr:nth-child(5) a").nil?

open('savedwebdata.txt', 'a') { |f|
  f.puts "#{name}\t#{address}\t#{website}\t#{Time.now}"
}
  end
end
Run Code Online (Sandbox Code Playgroud)

ruby screen-scraping nokogiri

5
推荐指数
1
解决办法
2345
查看次数

基于对象属性将java集合拆分为子集合

我有一个MyObjects列表... MyObject {int id,String name}.现在我想将列表拆分为具有相同"id"值的子列表,任何人都可以建议这样做的有效方法.

java collections

6
推荐指数
3
解决办法
1万
查看次数

为什么我们应该在asp.net的sql查询之前编写@?

假设我们想从数据库中选择数据,然后我们为此编写查询.

例:

SqlConnection con=new SqlConnection(Connetion name)
string selectPkId = @"SELECT PK_ID FROM TABLE"
SqlCommand cmd=new SqlCommand(selectPkId ,con);
Run Code Online (Sandbox Code Playgroud)

所以,我的问题是为什么我们在sql查询之前基本上使用@.如果我之前没有使用@那么它再次正常工作(不会给出任何错误),那么使用"@"需要什么?请告诉我.

asp.net verbatim

2
推荐指数
1
解决办法
1150
查看次数

移动Safari/Webkit On Window焦点事件

我想知道当用户切换回"你的"页面窗口时是否有可能获得一个事件?当用户打开新选项卡然后切换回页面选项卡或用户关闭safari然后再次打开时,可能会发生这种情况.

我希望能够在收到此活动后更新页面上的内容.我现在使用setInterval来实现这一点,但是当用户关注窗口时没有延迟会很好.

谢谢!

javascript mobile-safari mobile-webkit

8
推荐指数
1
解决办法
2792
查看次数