Q/A 11-Nov-2019 (Thread Safety)

Question:

We have a desktop application(client) that can communicate to many devices(servers) at the same time. A different thread pool is used for each device communication, so they do not block each other or the main thread. Because of the issue earlier, I am using the same http object for all communications. Most of the requests are very quick (~ 100 ms). Today I realized that the http.SynchronousRequest is actually synchronized, it is queuing all requests and running them one by one. A long  processing request, such as a file upload on a device blocks all communications to other devices. So even if you use separate threads for different devices, calling this method in a thread blocks all other threads,  because they all call this synchronized method. I don’t think http.SynchronousRequestAsync method will help, as it will call http.SynchronousRequest in a thread pool which I am already doing.

Is the above locking behaviour intended or is it a bug? Is there a way to send several requests and get responses at the same time via the same http object or any other?

Answer:

The Chilkat classes are thread-safe, and the way thread-safety is achieved is to allow only 1 method call at a time into a given instance of an object.  For example, if N  threads each have a reference to Chilkat object X, and the threads simultaneously call a method on instance X, then the 1st call to arrive is allowed to proceed and the others wait.  When the 1st call returns, then the next call is allowed entry, and so on.  There are some exceptions.  For example, the Chilkat.Socket class has finer-grained thread safety to allow one thread to read while another writes (but multiple threads cannot simultaneously read, or simultaneously write).

When a Chilkat async method is called (i.e. a method name ending in “Async”, such as SynchronousRequestAsync), then Chilkat is creating a background thread that makes the call — but it’s still a call into the object instance (but from a background thread), and it must wait it’s turn in the same way as if the application created its own threads and each thread called the synchronous method.

Also.. if you think about it..  for any class where the protocol is a conversation  (HTTP, FTP, IMAP, SMTP, POP3, etc.), it would not make sense to be trying to have multiple conversations on the same connection.   (SSH is different because the conversations are on logical channels.)

You can send multiple HTTP requests simultaneously if each thread has its own instance of an HTTP object.  This way, each thread has its own connection with its own conversation.