Multi-threading using a share socket in Haskell - multithreading

In Haskell, can the same socket be shared between two threads? Essentially, what I am trying to do is create a thread to write to a socket and another to read from the socket.
The read thread would be a loop that constantly wait, monitor and pick up any messages generated by the socket.
Another thread (the write thread) would send code to the socket.
Please advise.

Yes, this is allowed.

Related

Can I send() from one thread and recv() from another on the same ZeroMQ REQ/REP Socket?

As the documents say, ZMQ sockets are not thread-safe. So I assume the answer to the title is 'No'.
Still, I can't figure out how to implement a non-blocking request-reply pattern using ZMQ:
Specifically, the client has a main thread which goes on about its business processing messages from a (thread-safe) message queue. The messages come from various sources, such as network, timers, I/O etc.
Occasionally the main thread wishes to send a request to a remote server, but it does not want to wait for a response (which may take a while to arrive).
Normally, I would use two threads:
The main message-processing loop thread. This will send() request on the REQ/REP socket
An auxiliary listener thread which will wait for the response from the server. This will use a blocking recv() on the socket, and push the responses to the main thread's queue.
How would I achieve this using ZeroMQ? Should the auxilary thread open an inproc socket and listen to messages from the main thread?
Actually, single thread is enough. Just send request to the server and poll messages with zmq_poll().
This model is fine if one request at a time is suffucient. If you need so send multiple requests and read replies asynchronously, use DEALER socket instead of REQ. Just send some requestId as the first frame, then add empty delimiter frame, then send actual request.
Chapter 3 of the guide has more details about REQ/REP message envelopes: http://zguide.zeromq.org/php:chapter3
Please let me know if this isn't clear enough, I will probably extend my answer with few code samples.

win32: waitable event object for GUI messages?

Is it possible to create an event object that becomes signalled when GUI message(s) is/are available, like one can create event objects that signal the presence of data on a socket for instance? The idea would be to use a WaitMultipleEvents for either something happening on the GUI or a request arriving on a TCP/IP socket.
I'd delegate the whole client/server comm stuff to a background thread if this weren't a QuickTime app (QuickTime is rather quirky where multithreading is concerned, on win32 at least).
BTW, can one do things like moving/resizing/renaming windows created on another thread, using the dedicated functions - or would that require posting messages explicitly?
There's no waitable object for messages in your queue, but there is a wait function that will wait for a waitable object or a message in your queue. See MsgWaitForMultipleObjects.
There is no such event to signal that a Windows message is available. The solution is to get socket notifications in the form of a Windows message instead of an event. WSAAsyncSelect configures this mode in the socket. This let you use your message loop to get both GUI messages and socket notifications, all in one thread.
Thanks for your answers. Indeed, I was reminded elsewhere of MsgWaitForMultipleObjects, and use that (code extract:)

Sockets Winsock async blocking Read Write simultaneously

I have a client server arch and I am using blocking win sockets. I have a read and a write thread both on the server and on the client side.
Say Client is waiting (blocked) on a read() call for server to write stuff to,
can The client write something to that socket while it is blocked on a read() call from another thread.
So does block affect full duplex bidirectonal sockets ? I understand that I will block on the read but why would it also block on the write()?
or in order for this to work do I have to use select() or poll()?
Thanks
can The client write something to that socket while it is blocked on a read() call from another thread
Yes, this is no problem at all.

Creating a simple Linux API

I have a simple application on a OpenWRT style router. It's written in C++ currently. The router (embedded Linux) has very limited disk space and RAM. For example there is not enough space to install Python.
So, I want to control this daemon app via the network. I have read some tutorials on creating sockets, and listening to the port for activity. But I haven't been able to integrate the flow into a C++ class. And I haven't been able to figure out how to decode the information received, or how to send a response.
All the tutorials I've read are dead ends, they show you how to make a server that basically just blocks until it receives something, and then returns a message when it got something.
Is there something a little more higher level that can be used for this sort of thing?
Sounds like what you are asking is "how do I build a simple network service that will accept requests from clients and do something in response?" There are a bunch of parts to this -- how do you build a service framework, how do you encode and decode the requests, how do you process the requests and how do you tie it all together?
It sounds like you're having problems with the first and last parts. There are two basic ways of organizing a simple service like this -- the thread approach and the event approach.
In the thread approach, you create a thread for each incoming connection. That thread reads the messages (requests) from that connection (file descriptor), processes them, and writes back responses. When a connection goes away, the thread exits. You have a main 'listening' thread that accepts incoming connections and creates new threads to handle each one.
In the event approach, each incoming request becomes an event. You then have event handlers that processes these events, sending back responses. Its important that the event handlers NOT block and complete promptly, otherwise the service may appear to lock up. Your program has a main event loop that waits for incoming events (generally blocking on a single poll or select call) and reads and dispatches each event as appropriate.
I installed python-mini package with opkg, which has socket and thread support.
Works like a charm on a WRT160NL with backfire/10.03.1.

Socket fd in multithreading

Is it ok to use same socket fd in multiple threads ? (over linux)
It depends on what you expect to happen, but yes you can. If you are reading UDP packets that are each a complete message this can work well. If you are reading data streams from TCP this will probably not work well.
You can have multiple threads accepting incoming connections on a socket and that can work pretty well as each thread gets one connection.
You will run into concurrency problems trying to send() or recv() (SOCK_STREAM) from multiple threads. You should use a critical section or some other means of creating serial access to send() and recv().
One standard thing to do with threads is to have the master listen() & accept(), then pass off the socket to a client thread that sends/receives/processes from that one socket. The client thread is responsible for calling close() or shutdown().

Resources