Thread synchronization context
mireczech
mireczech11 at gmail.com
Tue Oct 22 11:33:06 UTC 2019
Hello everyone,
Recently, a few days ago I discovered dlang and I really like it.
It's a programming language which meets to my idea of what a
modern programming language should look like. I decided to do one
project in it.
Now I am addressing one problem. I have two threads, one main and
the other for a communication. How can I call a method from a
communication thread in the context of the main thread? Something
like the SynchchronizationContext class in c#.
An example:
alias PacketEventHandler = synchronized void delegate (Object
sender, PacketEventArg e);
public class TcpClient : Thread
{
public this()
{
_socket = new Socket(AddressFamily.INET, SocketType.STREAM);
_socket.blocking = false;
PacketReceived = &client_packetReceived;
super(&Execute);
}
protected void Execute()
{
_socket.connect(new InternetAddress(_address, _port));
while (!_terminated)
{
_socket.send(dataToSend);
ubyte[] receivedBuffer = new ubyte[_packetSize];
auto received = _socket.receive(receivedBuffer)
if (PacketReceived !is null)
PacketReceived(this, new PacketEventArg(receivedBuffer));//that
delegate I would like to call from the context of main thread
}
}
public PacketEventHandler PacketReceived;
void client_packetReceived(Object sender, PacketEventArg e)
{
writeln("received byte0: ", e.dataPacket[0]);
}
}
Thank you for any answers
More information about the Digitalmars-d-learn
mailing list