简体   繁体   English

为什么本地创建的结构不能发送到另一个线程?

[英]Why locally created struct can't be send to another thread?

why in DI can't send to another thread through Tid.send local instances of structs? 为什么在DI中无法通过Tid.send本地结构实例发送到另一个线程? I would like to make simple handling of thread communication like this: 我想像这样简单处理线程通信:

void main()
{
    ...
    tid.send(Command.LOGIN, [ Variant("user"), Variant("hello1234") ] );
    ...
}

void thread()
{
    ...
    receive(
       (Command cmd, Variant[] args) { ... })
    )
    ...
}

If I understand it correctly, D should create the array of Variants in the stack and then copy content of the array the send function right? 如果我理解正确,D应该在堆栈中创建变量数组,然后复制数组内容的发送功能吗? So there should not be any issues about synchronization and concurrency. 所以不应该有任何关于同步和并发的问题。 I'm quiet confused, this concurrency is wierd, I'm used to code with threads in C# and C. 我很安静,这种并发很奇怪,我习惯用C#和C中的线程编码。

Also I'm confused about the shared keyword, and creating shared classes. 此外,我对shared关键字和创建共享类感到困惑。 Usualy when I try to call method of shared class instance from non-shared object, the compiler throws an error. 通常,当我尝试从非共享对象调用共享类实例的方法时,编译器会抛出错误。 Why? 为什么?

you should idup the array and it will be able to go through, normal arrays are default sharable (as they have a shared mutable indirection) 你应该idup数组,它将能够通过,普通数组是默认可共享的(因为他们有一个共享的可变间接)

as is the compiler can rewrite the sending as 因为编译器可以重写发送为

Variant[] variants = [ Variant("user"), Variant("hello1234") ] ;
tid.send(Command.LOGIN, variants);

and Variant[] fails the hasUnsharedAlias test 和Variant []未通过hasUnsharedAlias测试

you can fix this by making the array shared or immutable (and receiving the appropriate one on the other side) 你可以通过使数组共享或不可变(并在另一侧接收适当的数组)来解决这个问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM