Skip to content
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

Open
parastuffs opened this issue Apr 28, 2022 · 2 comments
Open

[Multithreading lab 9] Missing free() #34

parastuffs opened this issue Apr 28, 2022 · 2 comments
Labels
bug Something isn't working

Comments

@parastuffs
Copy link
Contributor

The example code is missing a free(arg).

@parastuffs parastuffs added the bug Something isn't working label Apr 28, 2022
@parastuffs
Copy link
Contributor Author

@f380cedric highlights the following points/remarks/concerns about the code snippet in the handout:

  1. It is not strictly necessary to create the pthread_t *tid and Args *arg on the heap (using malloc), they could as well be created on the stack of the main function. There is no particular concern that they could be lost before the end of the thread if the main function was to quit early as we wait for them through a pthread_join anyway.
    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.
  2. In void *myThreadFun(Args *vargp), the mutex should cover the printf as well as it's accessing the global variable g.
  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?

@f380cedric
Copy link
Contributor

f380cedric commented May 4, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants