Passing stack variables to pthread_cleanup_push

tomKPZ

I have a thread that uses a file descriptor and must close() the file descriptor when cancelled. The pseudocode looks like:

static void thread_cleanup(void *args)
{
    int *fd = (int *)args;
    close(*fd);
}

void *thread(void *arg)
{
    int fd = open(...);
    ...
    pthread_cleanup_push(thread_cleanup, &fd);
    ....
}

One option I had was to pass fd casted to a void * from thread, and have thread_cleanup cast it back to an int, but this may cause problems if sizeof(int) != sizeof(void *). My question is: is passing stack variables as in the pseudocode safe when used in this fashion?

R.. GitHub STOP HELPING ICE

Your concern about sizeof seems to be based on some confusion. No conversion between int and void * is taking place, so whether they are the same size, or whether the values of one can be represented in the other, is irrelevant. What's happening is that the address of fd, a pointer value of type int *, is being converted to void * and back to int *. This is the whole point of void * and is perfectly correct usage.

As for whether it matters that the pointed-to object is "on the stack" (an object with automatic storage duration in the function where the cleanup handler is pushed/popped), the answer is yes, but your usage is okay.

Formally (as the current specification in POSIX is written), any object whose lifetime did not end before cancellation was acted upon still exists and can be accessed from all the cleanup handlers. However, this seems to be a mistake in the standard, as it conflicts with the Rationale document which supports unwinding-based implementations (like most real-world implementations) where cancellation cleanup handlers execute at the block context where they were pushed. Since the object int fd; in the function thread has lifetime associated with the containing block outside the pthread_cleanup_push block, it's perfectly safe to access it from the cleanup handler thread_cleanup.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

No matching function call when passing member function of a class to pthread_cleanup_push()

From Dev

No matching function call when passing member function of a class to pthread_cleanup_push()

From Dev

Passing variables with include in salt-stack

From Dev

pthread_cleanup_push causes Syntax error

From Dev

pthread_cleanup_push causes Syntax error

From Dev

pthread memory leak with stack variables

From Dev

pthread_cleanup_push with pthread_mutex_unlock

From Dev

pthread_cleanup_push handler not working for SIGINT (Ctrl C)

From Dev

What will happen if we return between pthread_cleanup_push and pthread_cleanup_pop in Unix? And why?

From Dev

How does gcc push local variables on to the stack?

From Dev

MEAN.JS stack $http.post().success() passing variables into success() anonymous function scope issue

From Dev

Pushing variables to Stack and Variables living in the Stack difference?

From Dev

Passing list (stack) in backtracing in python

From Dev

Passing variables in jQuery

From Dev

Passing uninitialized variables safety

From Dev

Passing Variables into a SQL String

From Dev

Passing variables through dplyr

From Dev

Passing variables in uiautomator

From Java

Jest passing variables to tests

From Dev

Passing variables in PHP pages

From Dev

passing class variables to decorator

From Dev

Passing Variables to a Bootstrap Modal?

From Dev

Passing variables to function using .on()

From Dev

Passing variables to ajax with onclick

From Dev

Passing variables to a YAML array

From Dev

Passing form variables as arrays

From Dev

Passing Variables In JavaScript

From Dev

Passing variables to a stored procedure

From Dev

passing and setting variables in a heredoc

Related Related

HotTag

Archive