douwen8424 2017-08-22 19:06
浏览 52
已采纳

尝试取消引用作为指向后端的struct对象的指针的接口,以便可以将值传递给函数

I have an interface that represents a pointer to a struct object after it has been initialized. I want to get access to the Value of the object that the interface references rather than the pointer so that I can pass it by value to a function as the interface type.

Here is an example of my issue: https://play.golang.org/p/_3vKThlj-V

I want to be able to pass val by value to the function EvalTest in my main function so that I can overwrite the pointer to the TestStruct object in another thread without causing pointer dereference issues in another thread.

The problem is that I'm running several thousand go routines against my interface object, but every 50 go routine calls I have to re-initialize my pointer due to a constraint of the system I'm integrating with. This seems to be leading to an inconsistent state where the new struct initialization is not completely finished when the go routines attempt to access it again. So the reason I wanted to pass it by value was so that I don't have to worry about the pointer being swapped out.

Any thoughts on this?

  • 写回答

1条回答 默认 最新

  • dongshou9343 2017-08-24 17:45
    关注

    This was solved by utilizing a channel for my integration api connection and passing a new pointer down the channel whenever it was successfully initialized. There were also some other optimizations that I was able to use to ensure that I was only replacing the pointer in the event of a successful connection. Here is an example of what I used that I got from Paul K in the Go slack channel.

    https://play.golang.org/p/AmbsfQOzol

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器