I'm trying to create a Dart program that will invoke multiple threads or Isolates. I need communication between this threads or Isolates, I have seen that there are some kind of channels and isolated channels (channels that work with ReceivePorts) but I need somethings that works similar to PubSub pattern, a Thread or Isolate will send a object (I need it to be a object, I know there is PubNub with Strings) and the rest of Threads or Isolates receives it.
Do you know something that could do the work? It would be great it there are some examples, I am relatively new with Dart language.
I know there is PubNub with Strings
Does it mean, Publishing only strings through PubNub? - It's not limited to strings. If that is stopper then custom objects can be published through pubnub dart sdk.
Related
This is software design question more than a coding one.
I am about to implement a feature where I can verify user's emails and phone numbers using Twilio's sms and voice apis.
My current implementation instantiates a Voice client at start up of the app and then I reuse this client whenever any user decides to verify email or voice.
Question: Is it a good idea to instantiate Twilio client once and then re-use it each time or should I create a new one each time it is needed?
I have browsed the Net for articles but haven't found something conclusive. Hoping to clarify here.
You are looking at whether the twillo client is thread-safe. A quick google search found this: Twilio Threaded Messages. I have not looked at the source myself, but I would consider this a likely answer that yes, it is thread-safe.
I'm not familiar with Twilio. But usually, since 3rd party API is out of our control, its stability, performance, etc, are all questions, and potentially, you might want to change to another service provider. So, firstly, try your best to decouple your own logic from 3rd ones. For instance, design an interface for this logic, and one implementation for Twilio.
Secondly, you need to test the Twilio client instance, ensure it could keep working for long time after instantiated, and if your programming language or runtime work in multi-thread way, you need to also test to make sure the instance could work properly when it is shared by multi-threads (if not, the instance is not threadsafe, you might consider using some mutex style locking on it).
Furthermore, if the 3rd party services execution is not stable, or, takes time for execution, etc, and specifically, for your email/sms verification case, it is not necessary to call the services synchronously and wait for responses. You could consider to use a worker queue, putting all tasks to the queue, and create some workers, running in asynchronous threads, to get tasks from queue and execute.
Could you give more explanations on MvvmCross multithreading?
ViewModel calls to Views are safe, so there must be no any conflicts.
However, IMvxMessanger has SubscribeOnThreadPoolThread and also SubscribeOnMainThread (except just Subscribe), which are not really clear for me when to use them.
Also, what's about multithreading inside of ViewModel (for instance, if two web-requests are activated simultaneously and on their results each of them tries to access my dataservice (for instance, writing data to database))?
(Or there are some other such special situations you know from your experiense).
Thank you!
For the single technical question about the differences between the subscribe methods on the messenger, these are explained in the XML comments on the interface (but are also largely self-explanatory anyway)
subscribe on main thread - messages will be received on main thread
subscribe on thread pool thread - messages will be received on a thread pool thread
subscribe - messages will be received, no assumptions should be made about which thread
Xml comments at - https://github.com/slodge/MvvmCross/blob/v3/Plugins/Cirrious/Messenger/Cirrious.MvvmCross.Plugins.Messenger/IMvxMessenger.cs#L15
For the rest of the question: as an app developer, you are free to use threading and async in your code - and the normal c# and .net multithreading objects are there for you to use (or a portable subset of them) - synchronising access to a resource is just a normal development decision and technique.
I'm writing a server, and decided to split up the work between different processes running node.js, because I heard node.js was single threaded and figured this would parallize better. The application is going to be a game. I have one process serving html pages, and then other processes dealing with the communication between clients playing the game. The clients will be placed into "rooms" and then use sockets to talk to each other relayed through the server. The problem I have is that the html server needs to be aware of how full the different rooms are to place people correctly. The socket servers need to update this information so that an accurate representation of the various rooms is maintained. So, as far as I see it, the html server and the room servers need to share some objects in memory. I am planning to run it on one (multicore) machine. Does anyone know of an easy way to do this? Any help would be greatly appreciated
Node currently doesn't support shared memory directly, and that's a reflection of JavaScript's complete lack of semantics or support for threading/shared memory handling.
With node 0.7, only recently usable even experimentally, the ability to run multiple event loops and JS contexts in a single process has become a reality (utilizing V8's concept of isolates and large changes to libuv to allow multiple event loops per process). In this case it's possible, but still not directly supported or easy, to have some kind of shared memory. In order to do that you'd need to use a Buffer or ArrayBuffer (both which represent a chunk of memory outside of JavaScript's heap but accessible from it in a limited manner) and then some way to share a pointer to the underlying V8 representation of the foreign object. I know it can be done from a minimal native node module but I'm not sure if it's possible from JS alone yet.
Regardless, the scenario you described is best fulfilled by simply using child_process.fork and sending the (seemingly minimal) amount of data through the communication channel provided (uses serialization).
http://nodejs.org/docs/latest/api/child_processes.html
Edit: it'd be possible from JS alone assuming you used node-ffi to bridge the gap.
You may want to try using a database like Redis for this. You can have a process subscribed to a channel listening new connections and publishing from the web server every time you need.
You can also have multiple processes waiting for users and use a list and BRPOP to subscribe to wait for players.
Sounds like you want to not do that.
Serving and message-passing are both IO-bound, which Node is very good at doing with a single thread. If you need long-running calculations about those messages, those might be good for doing separately, but even so, you might be surprised at how well you do with a single thread.
If not, look into Workers.
zeromq is also becomming quite popular as a process comm method. Might be worth a look. http://www.zeromq.org/ and https://github.com/JustinTulloss/zeromq.node
I'm trying to learn the lock/mutex stuff using boost library, but all that I found from the internet is too abstract or complicated.
Would you guys recommend me some tutorials, which are easy to understand? Thanks.
I'm working on a project. Server-Client architecture.
The server can receive messages from the client or send messages to client. The server can also send messages using multiple threads.
I believe that I have to do thread synchronization to handle multiple threads for sending messages via the same connection, right?
Can you guys give me a simple pseudo code snippet?
Skip mutexes and threads, read about Boost.Asio if you are designing a client server architecture. Particularly, study the asynchronous design that it promotes with concurrency without the explicit use of threads.
I tend to use the following as my standard threading model, but maybe it isn't such a great model. What other suggestions do people have or do they think this is set up well? This is not for a high performance internet server, though performance is sometimes pretty critical and in those cases I use asynchronous networking methods and reuse buffers, but it is the same model.
There is a gui thread to run the gui.
There is a backend thread that handles anything that is computationally intensive (basically anything the gui can hand off that isn't pretty quick to run) and also is in charge of parsing and acting on incoming messages or gui actions.
There is one or more networking threads that take care of breaking an outgoing send into peices if necessary, recieving packets from various sockets and reassembling them into messages.
There is an intermediary static class which serves as an intermediary between the networking and backend threads. It acts as a post office. Messages that need to go out are posted to it by backend threads and networking threads check the "outbox" of this class to find messages to send and post any incoming messages in a static "inbox" this class has (regardless of the socket they arrive from, though that information is posted with the incoming message) which the backend thread checks to find messages from other machines it should act on.
The gui / backend threading interface tends to be more ad hoc and should probably have its own post office like class or some alternative intermediary?
Any comments/suggestions on this threading setup?
My primary concern is that you don't really want to lock yourself into the idea that there can only be one back-end thread. My normal model is to use the MVC at first, make sure all the data structures I use aren't inherently unsafe for a threaded environment, avoid singletons, and then profile like crazy, splitting things out as I go while trying to minimize the number of condition variables I'm leveraging. For long asynchronous tasks, I prefer to spawn a new process, particularly if it's something that might want to let the OS give it a differing priority.
This architecture sounds like the classic Model-View-Controller architecture which is usually considered as good.