Skip to content

Conversation

@bmoore06
Copy link
Owner

@bmoore06 bmoore06 commented Dec 1, 2021

During the week we went through the following topics: Synchronization, the context package, race conditions, how to fix them with sync.Mutex and sync.RWMutex, sync.Cond, sync.Once and sync.Pool. We needed to learn about the sync.WaitGroup because if we wanted to run multiple goroutines at once, this sync.WaitGroup allows us to wait for all of the goroutines to finish before the program continues. The sync.WaitGroup is used to track how many goroutines are still running, and know when they are finished. The main goroutine calls Add to set the number of goroutines to wait for. Then each of the goroutines runs and calls Done when finished. At the same time, Wait can be used to block until all of the goroutines have finished. In order to wait for all the goroutines to finish we need to use the sync.WaitGroup.Wait method in order to block until all of the tasks are completed. In order for the sync.WaitGroup to know how many goroutines it needs to wait for, we will need to add them to the sync.WaitGroup using the sync.WaitGroup.Add method. Finally, in order to end the sequence, we will need to call the sync.WaitGroup.Done exactly once for each number of items we add with sync.WaitGroup.Add.

In addition to the sync.Waitgroup we learned and applied the race conditions to the program as well. Race conditions are needed in order to avoid the program to crash by having two or more goroutines running at once and wanting to avoid them accessing the same resource at the same time. In order to run multiple goroutines at once and not crash the program, the race conditions are vital in detecting any sort of bugs that we would need to fix. In this program we needed to write race conditions especially with us performing multiple tasks all at once. We could not have every task be performed in a linear manor or else the program will crash due to the goroutines accessing the same resources at once. For this program we needed to have multiple goroutines running at a time since we needed the product to be built by an employee, and employee to notify the manager when the product is built and the manager to notify the client when the product is completed. We could not have each goroutine run at the same time because then the process could be messed up and the product may have been built wrong and not delivered to the client correctly.

It was especially difficult to implement all of the processes together, have them run in a fast and productive manner and also not crash the program at the same time. I found it difficult to implement the block and the race conditions and where they needed to go in the program in order to run the way it needed to in order to successfully complete a product.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants