How to/best practices to write a thread safe code?


  • 8
    A

    How to/best practices to write a thread safe code? Also how to identify if a code is thread safe or not?


  • 0

  • 2
    A

    @Reckful Thanks for the links but these links merely explains what is processes, threads, synchronization between threads and some other concepts.
    What I am interested in is a some sort of guidelines to be kept in mind while writing a program so that it is thread safe. By thread safe I mean when more than one thread executes the same function there shouldnt be any race condition or deadlock.
    Hope you got my question.


  • 1
    S

    I think 2 points to remember, at higher level:

    1. Are we dealing with any common data that is accessible among the threads, if so, how to maintain integrity of the data.
    2. Are we dealing with any common data structure that is accessible among threads, if so, how to serialize access to avoid any exceptions.

  • 0
    A

    To give a better context, I heard this problem being asked in Adobe. So if an interviewer asks you this question then what would you tell?


  • 4
    D

    First things first, concurrent programming is very different from parallel computing. Concurrent programming is a paradigm of software design and parallel computing deals with how processes are run in parallel.

    "In programming, concurrency is the composition of independently executing processes, while parallelism is the simultaneous execution of (possibly related) computations. Concurrency is about dealing with lots of things at once. Parallelism is about doing lots of things at once" - Rob Pike

    The bane of concurrent programming is shared mutability. Shared mutability of objects is the major reason for bottlenecks and slow code.
    Here is a great talk that explains it very well
    https://blog.golang.org/concurrency-is-not-parallelism

    Your answer should be,
    I will design in the application such that I will minimize shared mutability and try to give the example of the gopher burning books from the video above.


  • 4
    D

    Sounds like they are wanting you to speak about mutex's and semaphores. Lot's of information on the internet about this and usually it'll lead into the question "ok, what's the difference between a mutex and binary semaphore?" (They behave the same outside of how/when/where the lock is set).


  • 0
    S

    Synchronized of thread and process.
    IPC: mutex, spinlock, socket, Semaphore, pipe, FIFO, message queue, share memory


  • 0
    S

    @shuoy After that, they would ask you how to implement mutex or spinlock.....


  • 0

  • 1
    G

    This how I would approach this question.. I first start asking is it really necessary to make something a multithreaded application, because creating correct multithreaded application is hard even for experts.

    I would suggest some design pattern which will effectively avoid dealing with threads.

    • Keep you data/data structure immutable or make best effort to make it immutable.
    • Since the advent of non-blocking-io, there is very few reason to create truly multi-threaded application. Employ pattern like GCD (Grand Central Dispatch), where various non-blocking-io completion events happen from a single thread
    • Then finally suggest, that we can't always do this and multi-threaded is absolutely necessary in some cases and suggest various primitives we have to solve this problem.

  • 1
    L

    stack overflow has a documentation about threading, it includes some basic concept and code samples of threading. I think it's helpful for beginners :)
    https://stackoverflow.com/documentation/c%2B%2B/699/threading#t=201708132229326345895


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.