-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Multithreading lab 9] Missing free() #34
Labels
bug
Something isn't working
Comments
@f380cedric highlights the following points/remarks/concerns about the code snippet in the handout:
Why woul we need to store the heap reference again? |
On 04/05/22 07:59, Quentin Delhaye wrote:
1. It is not strictly necessary to create [...] `Args *arg` on the heap [...]
However, if you decide to replace the `Args *arg` with stack allocation, do it outside the loop, otherwise the reference is lost upon looping or leaving the loop scope.
You will need to allocate an array Args[].
Otherwise, you will rewrite the same stack space at each iteration.
The initialization can still be done inside the loop.
2. In `void *myThreadFun(Args *vargp)`, the mutex should cover the `printf` as well as it's accessing the global variable `g`.
Unless you don't care that a thread might do something to `g`
between the release of the mutex and the read operation.
3. As a rule of thumb, the argument type of a thread function is `void *`, as generic as it comes. However in this case, we know exactly what is the argument type and we won't use any other, so we might as well specify it.
4. The first operation of the thread function seems to be superfluous:
```
// Store the argument passed to this thread
Args *arg = vargp;
```
Why woul we need to store the heap reference again?
Other comments:
- Every function call that can fail should be handled
(e.g. malloc, pthread_create).
- `myThreadFun` should use `return` instead of `pthread_exit`. On
threads other than `main`, they are synonyms. `pthread_exit` seems
only useful in terminating main without terminating its children.
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The example code is missing a
free(arg)
.The text was updated successfully, but these errors were encountered: