pthread_cleanup_xx more comfortable use

This is a discussion on pthread_cleanup_xx more comfortable use within the C Programming forums, part of the General Programming Boards category; I receive errors if i don't put pthread_cleanup_push() and pthread_cleanup_pop() inside the same function I guess cause they're somehow implemented ...

  1. #1
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251

    pthread_cleanup_xx more comfortable use

    I receive errors if i don't put pthread_cleanup_push() and pthread_cleanup_pop() inside the same function
    I guess cause they're somehow implemented as macros and the compiler or linker pretends them to be like that
    Can someone give me a way to avoid this strong limitation??

    Thank you

    P.S. Also : they have to be at the some logical/conditional level
    Last edited by mynickmynick; 03-31-2009 at 02:18 AM.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,607
    Quote Originally Posted by IEEE Std 1003.1, 2004 Edition
    These functions may be implemented as macros. The application shall ensure that they appear as statements, and in pairs within the same lexical scope (that is, the pthread_cleanup_push() macro may be thought to expand to a token list whose first token is '<open-bracket>' with pthread_cleanup_pop() expanding to a token list whose last token is the corresponding '<close-bracket>' ).
    www.opengroup.org/onlinepubs/009695399/functions/pthread_cleanup_pop.html

    gg

  3. #3
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251
    As a matter of fact this
    Code:
    These functions may be implemented as macros. The application shall ensure that they appear as statements, and in pairs within the same lexical scope (that is, the pthread_cleanup_push() macro may be thought to expand to a token list whose first token is '{' with pthread_cleanup_pop() expanding to a token list whose last token is the corresponding '}' ).
    is very annoying. Could you suggest an alternative? I guess the only alternative is to implement it on my own with a big function CancelationHandler that takes as an argument a list of dynamically changed execution stack and by calling

    Code:
    pthread_cleanup_push(CancelationHandler()  , ArgsList);
    ..
    ..
    pthread_cleanup_pop(1);
    pthread_exit(0);
    where ArgsList can be changed anywhere in the code or other called functions, pushing or popping new functions and args as if i was calling there pthread_cleanup_xx

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,607
    >> Could you suggest an alternative?
    For what? You have to use push/pop in pairs and within the same scope. Why do you feel this is a "strong limitation"?

    gg

  5. #5
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251
    Quote Originally Posted by Codeplug View Post
    >> Could you suggest an alternative?
    For what? You have to use push/pop in pairs and within the same scope. Why do you feel this is a "strong limitation"?

    gg
    it is an evident limitation
    it would be useful to have pthread_cleanup_push()s inside functions and pthread_cleanup_pop()s inside other functions in completely different scope

    instead what i have to do is use only a couple of pthread_cleanup_pairs refering to a single MegaFunction implementing what i need (as explained above)

  6. #6
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,160
    Quote Originally Posted by mynickmynick View Post
    it is an evident limitation
    it would be useful to have pthread_cleanup_push()s inside functions and pthread_cleanup_pop()s inside other functions in completely different scope
    That would lead to an architecture which is difficult to comprehend. I think the limitation is doing you a favor. You can still break your operations down into functions, but you have to do it in a way that keeps all cleanup push/pop in the same lexical scope.

    This is no different from RAII in the C++ world, and no less useful.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  7. #7
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251
    may be my experience is low so i am not understanding well your point
    basically i would keep them inside the same lexical scope but at an higher level

    what i would like is something like:

    Code:
    InitSomething1();
    OpenSomething();
    ..
    do many things..
    ..
    
    CloseSomething();
    DeInitSomething();
    pthread_exit();
    
    
    InitSomething()
    {
    ..
    pthread_cleanup_push(..);
    ..
    }
    
    OpenSomething()
    {
    ..
    pthread_cleanup_push(..);
    ..
    
    }
    
    DeInitSomething()
    {
    ..
    pthread_cleanup_pop(1);
    ..
    }
    
    CloseSomething()
    {
    ..
    pthread_cleanup_pop(1);
    ..
    
    }
    This actually would be useful probably cause i have to work on code written by others and not by me, which has a structure i dislike but would better fit with this sort of draft (They're currently handling cancelation in a very rough way, so i am trying to put something cleaner on)

    On other code i wrote on my own i found it not so diffcult to use pthread_cleanup_xx in the straight forward way you say, only inside the ThreadFunction at the same scope
    Last edited by mynickmynick; 04-01-2009 at 01:55 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Gamecube Controllers >> Sweaty Hands
    By LordVirusXXP in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 12-27-2002, 05:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21