About Monkey 2 › Forums › Monkey 2 Programming Help › Fiber question
This topic contains 9 replies, has 4 voices, and was last updated by
EdzUp
1 year, 6 months ago.
-
AuthorPosts
-
August 19, 2017 at 8:20 pm #9926
If I create a Fiber to handle all ‘new’ connections with Socket.Accept in a loop which then passes the new client to another Fiber which continuously runs through the client list making sure its always updated my question is simply:
Would the Socket.Accept blocking when there are no new connections cause any issues with the Fiber, I havent used Fibers much and before I dive into it and hit a wall I wondered if there was anything holding the Fiber up would it cause any problems?
August 19, 2017 at 9:38 pm #9927The echoserver example launches a server fiber which in turn creates a fiber per connection.
I would imagine problems would arise if you don’t use fibers hence they solve problems rather than create them.
August 19, 2017 at 9:44 pm #9928ah I see now the server needs a thread per client connection. I was thinking it was like BlitzMax where you could have a socket accept in the main loop.
October 20, 2017 at 9:58 am #11216Another question
Can different Fibers access the same linked list?
For example if I have a linked list for a server called ‘Connections’ then when the connection Fiber get a connection request can I just get it to add the connection information to a connection list so the server can process the new connection or is that a MAV? The reason I ask is as Fibers all run at the same time in the background of the main loop I would wonder if they all accessed the same list then at some point there would be two or more accessing it which could cause corruption. Would that be the case or has it already been thought of?
October 20, 2017 at 6:14 pm #11222I’m very interested in this as well!
I thought of a server usage together with SQLite. Can there be a global list where all threads/fibers can access? At least read access?
October 20, 2017 at 6:38 pm #11225to be honest in a network environment you would need a list where you could read and write data to
October 20, 2017 at 7:09 pm #11226Yes you’re right. But for that you need some mutex stuff or similar lock mechanisms.
That’s why I mentioned sqlite because a database shouldn’t have problems with parallel write access.
October 20, 2017 at 7:17 pm #11227this is where Fibers get confusing there is so much confusion over what they do and how they can be used. As for the echo server example it doesn’t go into networking enough it’s just a client sending and receiving from a server the server doesn’t do anything with it. A chat client/server app would be MUCH better.
October 20, 2017 at 9:35 pm #11232The point of fibers is really that they allow you to perform blocking operations, without causing the entire program to halt.
So yes, if a fiber executes ‘Accept’ and there’s nothing trying to connect, it will block. And blocking will allow other fibers to run etc.
In general, there’s no need to synchronize access to global lists etc with fibers, because fibers only ‘switch’ at known times, eg: blocking for an Accept etc, blocking for a Read, blocking while waiting for a Process to finish etc.
You do have to be a tiny bit careful here, as its possible to indirectly call code that may cause a fiber switch without your knowledge. But even then, the switch will never occur in the middle of adding a stack element or whatever so it’s perfectly safe to use lists, stacks etc, even global ones, from any fiber. Ted2 makes extensive use of fibers and I never once had to really think about synchronizatoion issues when using fibers. Which is what largely makes them so cool.
October 20, 2017 at 9:41 pm #11233Ah OK I understand now it’s basically a snippet program that halts until it can continue :). This makes it much easier to get to grips with and one thing that hopefully will make it into the docs
-
AuthorPosts
You must be logged in to reply to this topic.