小编Gab*_*iel的帖子

stl vector并发读取线程安全吗?

我正在开发一个应用程序,其中大量的线程需要迭代一组字符串值,并尝试将其自己的数据与列表中的可用数据进行匹配.

我正在寻找以下用例:

  1. Vector初始化为几个std :: string类型的元素.(可以说对象名是strList).strList将在应用程序启动时初始化.
  2. 所有线程都将遍历strList以查看它的值是否与strList的至少一个元素匹配.
  3. 没有线程会尝试修改strList,它将严格用作只读对象.

那么请告诉我并发读取是否对矢量对象是线程安全的.我使用的是RHEL 6,gcc版本是4.5.x

c++ concurrency stl thread-safety language-lawyer

23
推荐指数
2
解决办法
2万
查看次数

如何在.NET中重用socket?

我试图重新连接到我已断开连接的套接字但由于某种原因它不会允许它,即使我使用参数"reuseSocket"设置为true调用Disconnect方法.

_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
_socket.Connect(ipAddress, port);
//...receive data
_socket.Disconnect(true); //reuseSocket = true
//...wait
_socket.Connect(ipAddress, port); //throws an InvalidOperationException:
Run Code Online (Sandbox Code Playgroud)

套接字断开连接后,您只能异步重新连接,并且只能连接到不同的EndPoint.必须在线程上调用BeginConnect,该线程在操作完成之前不会退出.

我究竟做错了什么?

.net c# sockets network-programming

14
推荐指数
2
解决办法
3万
查看次数

选择G元素中的路径并更改样式

基本上我试图让所有的路径除了一个被翻转为灰色的路径,而被选中的路径保持它的原始颜色.我已经能够将所有其他路径变为灰色,但是我遇到了"select.this"功能的问题并且实际上访问了我想要改变样式的路径.看来我实际上已经设法进入g组中的path元素,但是我在控制台中遇到错误

Uncaught TypeError: Property 'style' of object #<SVGGElement> is not a function
Run Code Online (Sandbox Code Playgroud)

相关代码:

    svg.selectAll("g.team")
    .on("mouseover",function(){
            console.log("I see you!");
            var lineName;
            var accuracy = 10;

            svg.selectAll("path.team.line").style("stroke","#C0C0C0"); 
            //set all to gray

            var selectedArray = d3.select(this);
            console.log(selectedArray);

            var selectGroup = selectedArray[0];
            console.log("should be group:"+selectGroup);

            var selectedLine = selectGroup[0];;

            selectedLine.style("color",function(d){  //let active keep color
            lineName = abbrDict[d.name];  //full name to be at end of line
            return color(d.name);
        });

            //get position of end of line
        var len = d3.select(this).children().node().getTotalLength();
        var pos = d3.select(this).node().getPointAtLength(len);  
        //append text …
Run Code Online (Sandbox Code Playgroud)

visualization d3.js

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

分配向量所花费的C++时间

我正在尝试加速一段总共运行150,000,000次的代码.

我使用"非常困"来分析它,它表明代码在这3个区域花费的时间最多,如图所示:

花时间

代码如下:

double nonLocalAtPixel(int ymax, int xmax, int y, int x , vector<nodeStructure> &nodeMST, int squareDimension, Mat &inputImage) {

    vector<double> nodeWeights(8,0);
    vector<double> nodeIntensities(8,0);
    bool allZeroWeights = true;
    int numberEitherside = (squareDimension - 1) / 2;
    int index = 0;
    for (int j = y - numberEitherside; j < y + numberEitherside + 1; j++) {
        for (int i = x - numberEitherside; i < x + numberEitherside + 1; i++) {

            // out of range or the centre pixel …
Run Code Online (Sandbox Code Playgroud)

c++ algorithm performance

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

C++ for循环不等待完成?

我在使用这段代码时遇到了一些麻烦:

for (long long int loop = 0; loop < 50000000; loop++)
{
    srand( (unsigned)time(NULL) ); // set the seed
    int r = abs(rand()); // stores the random seed for this loop
    int res = loop + r - r * r / r; // random operations with the loop number and the random seed.
    cout << "Loop number: " << loop << ". " << "Result: ";
    cout << res << endl;
}//this was missing
Run Code Online (Sandbox Code Playgroud)

如果运行该代码,您可以在控制台中非常清楚地看到它的输出仅每隔几秒进行一次计算.这是怎么回事?每个循环的数量应该完全不同,因为它使用随机数进行计算.相反,数字会在每个x循环运行时更改,然后它似乎只会在这些时间之间增加,它实际上会进行数学运算.

我是否需要指定我希望循环等到一切都完成后再继续?

c++ loops for-loop

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