Lecture 2: RPC and Threads
why use Threads
I/O Concurrency = using mutiple thread to call remote procedure, each thread will call remote i/o and wait for answer
Parallelism
Convience = do something in background or periodically
with out Threads
event driven programming = has single thread and single loop (like nodejs) , cant have parallelism , hard to write sequential programming ,
Thread chanllenges
threads share same memory = synchronous issue
Thread Coordination
channel = just sending data to other thread
sync.cond = give other thread a "kick" to perform its operation
wait group = waiting for every other thread to finish
crawler with shared memory (= fetch map structure)
done.Done() = decrement by 1 which is what "done.Add" added , we are calling this before ConcurrentMutex because it might fail during gorotuine operation
done.wait() = wait until done get to zero
u string = it uses copy of for_, u:= range urls , because outside u keeps changing and gorotuine might refer to other url which is not our intention
crawler with channel
notice worker thread(function) doesn't have any shared memory , doesn't need to worry about lock
ch is channel , worker is sending to that channel while master is receiving from that channel