How to/best practices to write a thread safe code? Also how to identify if a code is thread safe or not?
@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.
I think 2 points to remember, at higher level:
- Are we dealing with any common data that is accessible among the threads, if so, how to maintain integrity of the data.
- Are we dealing with any common data structure that is accessible among threads, if so, how to serialize access to avoid any exceptions.
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?
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
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.
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).
Synchronized of thread and process.
IPC: mutex, spinlock, socket, Semaphore, pipe, FIFO, message queue, share memory
@shuoy After that, they would ask you how to implement mutex or spinlock.....
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.
stack overflow has a documentation about threading, it includes some basic concept and code samples of threading. I think it's helpful for beginners :)
Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.