小编5go*_*der的帖子

不了解Java原子包中的CAS操作

我正在研究Java书籍中的并发包。我不太了解本书中有关CAS操作的内容。下面的代码示例是counter本书中的线程安全类。

public class Counter{
    private AtomicInteger count = new AtomicInteger();
    public void increment(){
        count.getAndIncrement();    //atomic operation
    }
    ....
}
Run Code Online (Sandbox Code Playgroud)

这是书中所说的。

实际上,即使诸如这样的方法也要getAndIncrement()花费几个步骤来执行。此实现现在是线程安全的,因此称为CAS。CAS代表“比较并交换”。大多数现代CPU都有一组CAS指令。现在正在发生的事情的基本概述如下:

  1. 存储在count中的值将被复制到一个临时变量中。
  2. 临时变量增加。
  3. 将当前计数的值与原始值进行比较。如果未更改,则将旧值替换为新值。

好吧,我明白它关于多个步骤的说法。我不太了解的是枚举步骤中正在发生的事情。

  1. 存储在count中的值将被复制到一个临时变量中。

该临时变量在哪里?它在主存储器中,注册吗?还是特定于CPU体系结构?

  1. 将当前计数的值与原始值进行比较。如果未更改,则将旧值替换为新值。

原始值存储在哪里?不能是临时变量。那个人正在被修改,对吧?我想念什么?

谢谢

java atomicity compare-and-swap

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

如何使@NotNull抛出运行时异常?

如果我将null作为参数传递给@NotNull注释,是否有任何框架可以抛出异常?我不是指静态分析而是运行时检查.

如果没有,如何实施呢?

java

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

类错误不存在默认构造函数

一些简单的代码:

class Thing {
public:
    int num;
    Thing(int num) { 
        this->num = num; 
    }
};

class Stuff {
public:
    Thing thing;  // an instance of thing is declared here but it cannot construct it
    Stuff(Thing thing) {
        this->thing = thing;
    }
};

int main() {
    Thing thing = Thing(5);
    Stuff stuff = Stuff(thing);
}
Run Code Online (Sandbox Code Playgroud)

所以在这里,我试图弄清楚我应该如何在Stuff的构造函数中使用Thing的新实例而不指向它,因为我希望Stuff拥有自己的副本.当然,我不能声明我上面的东西,因为它试图初始化它.

如何通过构造函数解决将新对象副本分配给类变量的问题?

确切的错误是:

In constructor 'Stuff::Stuff(Thing)':
error: no matching function for call to 'Thing::Thing()'
  Stuff(Thing thing){ this->thing = thing; }

candidate expects 1 argument, 0 provided
Run Code Online (Sandbox Code Playgroud)

c++ constructor

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

如何从ByteBuffer中获取已使用的byte[]

该类java.nio.ByteBuffer有一个ByteBuffer.array()方法,但是它返回一个数组,该数组是缓冲区容量的大小,而不是已使用的容量。因此,我遇到了很多问题。

我注意到 usingByteBuffer.remaining()给出了缓冲区当前正在使用的字节数,所以基本上我正在寻找的是一种byte[]仅获取正在使用的字节的方法。(即,正在显示的字节ByteBuffer.remaining()

我尝试了一些不同的事情,但我似乎失败了,我能想到的唯一解决方法是使用ByteBuffer剩余缓冲区的分配大小创建另一个,然后向其中写入(x)字节。

java bytebuffer

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

C隔离文本文件中的"仅字符串"

我有一个文本文件,其中有1个单词后跟~100个浮点数.浮点数由空格,制表符或换行符分隔.此格式在整个文本文件中重复多次.

例如,这就是文本文件的样子:

one 0.00591 0.07272 -0.78274 ... 
0.0673 ...
0.0897 ...
two 0.0654 ...
0.07843 ...
0.0873 ...
three ...
...
...
Run Code Online (Sandbox Code Playgroud)

我的问题是,如何计算文件中的单词数量,我尝试使用fscanf但是一旦它读取第一个单词,之后我必须跳过所有浮点数直到下一个单词.

任何帮助将非常感激.

谢谢.

c parsing

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

哪个是初始化std :: map的最佳方法,它的值是std :: vector?

我有以下内容:

std::map<std::string, std::vector<std::string>> container;

要添加新项目,请执行以下操作:

void add(const std::string& value) {
    std::vector<std::string> values;
    values.push_back(value);
    container.insert(key, values);
}
Run Code Online (Sandbox Code Playgroud)

有没有更好的方法来增加价值?

谢谢

c++ stl vector map c++11

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

如果我不清除stringstream会发生什么?

我在TopCoder中做了一个图形问题,不知怎的,我的程序仍然输出错误的答案,即使我认为一切都应该没问题.我花了几个小时的时间在我的逻辑中寻找错误,事实证明,问题在于其他地方.这是一段代码,我有一个问题:

int x, y;
stringstream ssx;
stringstream ssy;
for (int i = 0; i < connects.size(); i++){
    neighbours.push_back(vector<int> ());
    edges_cost.push_back(vector<int> ());
    ssx.str(connects[i]);
    ssy.str(costs[i]);
    while (ssx >> x && ssy >> y){
        neighbours[i].push_back(x);
        edges_cost[i].push_back(y);
    }
    // The problem lied here. Apparently without these 2 lines
    // the program outputs wrong answer. Why?
    ssx.clear();
    ssy.clear();
}
Run Code Online (Sandbox Code Playgroud)

正如您在评论中看到的那样,我设法解决了这个问题.但我不知道为什么我需要清除那些串流.如果我不这样做,到底发生了什么?

c++ stringstream

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

空的"else"子句在C++中是否有任何意义?

以下2个代码在功能上有什么区别吗?如果没有,是否有首选风格?

int main()
{
    int i=11;

    if (i > 100)
    {
        i = 100;
    }
    else if (i < 0)
    {
        i = 0;
    }

    cout << i << endl;
}
Run Code Online (Sandbox Code Playgroud)

int main()
{
    int i=11;

    if (i > 100)
    {
        i = 100;
    }
    else if (i < 0)
    {
        i = 0;
    }
    else
    {
    }

    cout << i << endl;
}
Run Code Online (Sandbox Code Playgroud)

换句话说,我的问题是,else如果我不想让它做任何事情,是否有任何意义?

c++ if-statement

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

将文本(单个字母)附加到文本文件中每行的末尾

下面是我正在使用的文本文件的示例:

437868313,2436413,Wyatt,Trenton,08/21/2003,211000010262002,211000010262002,2014,01,54435A000,510,Social Studies (Grade 5),08/14/2013,5-2,02,0,02,02,01,,,,,,100,05/29/2014,
437868313,2436413,Wyatt,Trenton,08/21/2003,211000010262002,211000010262002,2014,01,53235A000,500,Science (Grade 5),08/14/2013,5-2,02,0,02,02,01,,,,,,100,05/29/2014,
437868313,2436413,Wyatt,Trenton,08/21/2003,211000010262002,211000010262002,2014,01,58035A000,560,Physical Education (Grade 5),08/14/2013,5-2,02,0,02,02,01,,,,,,1,05/29/2014,
Run Code Online (Sandbox Code Playgroud)

我试图简单地将字母'S'添加到每一行的末尾.所以,上面有3条记录.在2014年5月29日之后,我想插入S.所以每条记录看起来像:

437868313,2436413,Wyatt,Trenton,08/21/2003,211000010262002,211000010262002,2014,01,54435A000,510,Social Studies (Grade 5),08/14/2013,5-2,02,0,02,02,01,,,,,,100,05/29/2014,S
Run Code Online (Sandbox Code Playgroud)

我意识到这将是如此简单的转换为CSV和使用excel,但我在传输回到txt时遇到各种格式问题.想用python来解决它.我正在尝试使用append,据我所知,write会覆盖我现有的文件:

myFile = open("myFile.txt", "a")
    for line in myFile:
        myFile.write('S')
Run Code Online (Sandbox Code Playgroud)

我不经常使用python,我想知道我如何索引它所以它从第2行开始,并在逗号之后追加行的最后一行,就像我上面提到的那样.

python csv text

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

如何编辑默认的makefile

我可以编辑默认的makefile吗?我想将-std=c++11标志添加到默认的makefile,以便make命令将编译和构建C++ 11程序.

例如,当前,当我尝试使用该make命令编译程序时,会发生以下错误:

g++ a.cpp -o a

a.cpp: In function ‘int main()’:

a.cpp:118:12: error: ISO C++ forbids declaration of ‘it’ with no type [-fpermissive]

  for(auto &it : v) cout << it << endl;
            ^

a.cpp:118:17: warning: range-based ‘for’ loops only available with -std=c++11 or -std=gnu++11
  for(auto &it : v) cout << it << endl;
                 ^

<builtin>: recipe for target 'a' failed

make: *** [a] Error 1
Run Code Online (Sandbox Code Playgroud)

c++ linux makefile

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