小编mGu*_*Guv的帖子

GC如何使用IEnumerator和yield?

我知道枚举器和yield关键字可用于帮助执行异步/交错操作,因为您可以调用MoveNext()以运行下一个代码块.

但是,我真的不明白Enumerator对象是什么.使用枚举器范围的内存在哪里?如果你没有MoveNext()一个枚举器,它最终会得到GC吗?

基本上,我正在尝试保持我的GC命中,因为我可能使用大量的枚举器和GC可能是Unity内部的一个问题,特别是由于它使用的旧版Mono.

我试图描述这个,但仍然无法绕过它们.我不理解枚举器发生的范围/引用.我也不明白当你从一个产生的函数创建一个枚举器时,它是否被创建为对象.

以下示例更好地显示了我的困惑:

// Example enumerator
IEnumerator<bool> ExampleFunction()
{
    SomeClass heavyObject = new SomeClass();
    while(heavyObject.Process())
    {
        yield return true;
    }

    if(!heavyObject.Success)
    {
        yield return false;
    }

    // In this example, we'll never get here - what happens to the incomplete Enumerator
    // When does heavyObject get GC'd?
    heavyObject.DoSomeMoreStuff();
}

// example call - Where does this enumerator come from? 
// Is something creating it with the new keyword in the background?
IEnumerator<bool> enumerator = …
Run Code Online (Sandbox Code Playgroud)

c# garbage-collection

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

如何将主机名转换为增强地址或端点?

我在Boost上使用的是C ++ Redis库。(https://github.com/nekipelov/redisclient

要连接,我必须给它一个tcp端点:

boost::asio::ip::tcp::endpoint
Run Code Online (Sandbox Code Playgroud)

或地址+端口

boost::asio::ip::address, unsigned short
Run Code Online (Sandbox Code Playgroud)

目前,我以:

boost::asio::ip::address address = boost::asio::ip::address::from_string(someIPVariable);
Run Code Online (Sandbox Code Playgroud)

并将其与端口一起通过,它可以正常工作并连接。但是,我现在需要按主机名而不是IP进行操作。如果仅将主机名放在上面的行中,则会抛出异常,因为我认为它需要IP地址。

我习惯将连接指定为,("IP OR Hostname", port)因此我不太确定这里需要什么。我检查了两者的构造函数,以查看是否可以将主机名+端口转换为所需的名称,但找不到任何内容。

c++ boost

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

有没有办法处理任何类型的集合,而不是仅仅依赖于Array,List等?

此示例适用于名为"WriteLines"的方法,该方法接受字符串数组并将其添加到异步文件编写器.它很有用,但我很好奇是否有一种有趣的方法来支持-any-字符串集合,而不是依靠程序员转换为数组.

我想出了类似的东西:

public void AddLines(IEnumerable<string> lines)
{
    // grab the queue
    lock (_queue)
    {
        // loop through the collection and enqueue each line
        for (int i = 0, count = lines.Count(); i < count; i++)
        {
            _queue.Enqueue(lines.ElementAt(i));
        }
    }
    // notify the thread it has work to do.
    _hasNewItems.Set();
}
Run Code Online (Sandbox Code Playgroud)

这似乎有效,但我不知道它有任何性能影响,或任何逻辑含义(订单会发生什么?我认为这将允许甚至无序集合工作,例如HashSet).

是否有更可接受的方式来实现这一目标?

c# collections performance

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

标签 统计

c# ×2

boost ×1

c++ ×1

collections ×1

garbage-collection ×1

performance ×1