Coder Social home page Coder Social logo

Thread Safety about cparse HOT 4 CLOSED

cparse avatar cparse commented on July 20, 2024
Thread Safety

from cparse.

Comments (4)

VinGarcia avatar VinGarcia commented on July 20, 2024

Uhm, so what you have is multiple threads writing to the same TokenMap or PackToken at the same time. So I can try to think of some alternatives, but I might need more info to understand which options we have.

One possible option might be to just change the architecture of the project so that threads communicate using a safe channel, as in "passing messages" instead of "sharing variables", but this depends on the specific problem.

A strategy that I love to use in other languages is the BlockingQueue strategy, where you only send messages from one thread to the other using these queues, if the queue has a max limit and it is full the thread sleeps. Then on the other side if a thread tries to read from the queue and it is empty it sleeps, otherwise, it just gets the data and continues.

You could for example create a new "channel" or "blocking_queue" type in your language and use it for synchronization:

This strategy would allow you to keep the packTokens as fast as they normally are and only pay for the extra synchronization price when using the channels.

However, after a quick search, it doesn't look like we have a standard implementation of that concept available neither on the std library nor on boost =/. But we still have some code snippets online that might be useful if you are willing to implement that on your own code:

from cparse.

VinGarcia avatar VinGarcia commented on July 20, 2024

If this solved your problem I will close this issue (no hurry though)

from cparse.

sarthaku21 avatar sarthaku21 commented on July 20, 2024

Hi,
I have introduced a recursive lock in packToken that ensures safe access. The problem is when we try to run calculator in a parallel thread. The = operation in packToken does a delete base, which cause the calculator to give a segmentation fault when resolving refTokens. I am still not able to solve this efficiently.
The application contains multiple threads, some updating packTokens, other evaluating expressions using calculator to do some tasks.
I am using the tokenMap as a shared memory between these threads to communicate. I will go ahead and limit access when calculator is doing it's job. But, I think it can be done more efficiently.
Thanks.

from cparse.

VinGarcia avatar VinGarcia commented on July 20, 2024

Yeah, so this is just my opinion, but I really think that adding a lock on every packToken might be the wrong approach. For example, I don't think there is any programming language that has built-in locks for every variable exactly because this is very inefficient.

So if efficiency is a concern I would recommend either using explicit locks, which are already available in C++ and should be easier to add to the library than the current approach, or trying some other strategy like the channels I mentioned.

Another interesting strategy is to use immutable structures whenever you can, which is the strategy used by the Elixir programming language for preventing this type of problem.

About the operator=, since you are already forking the library you could try to update the delete method of each type to prevent this segmentation fault. But as I said, this might be complicated.

from cparse.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.