Page 6 of 6 FirstFirst ... 456
Results 51 to 53 of 53

Thread: Problem with Programming Threads

  1. #51
    Join Date
    Mar 2005
    Location
    Dunedin, NZ
    Beans
    559
    Distro
    Kubuntu 7.10 Gutsy Gibbon

    Re: Problem with Programming Threads

    I'm not trying to dodge it, I was trying to see if you would come to the realisation as I did.

    What happens to this code when f() throws?
    Code:
    template <typename Func>
    void
    unbuffered_io_call(Func f) {
    	termio curr, old;
    	::ioctl(0, TCGETA, &curr);
    	old = curr;
    
    	curr.c_lflag = 0;
    	curr.c_iflag = 0;
    	curr.c_cc[VMIN] = 1;
    	curr.c_cc[VTIME] = 0;	
    	::ioctl(0, TCSETA, &curr);
    
    	f();
    
    	::ioctl(0, TCSETA, &old_);
    }
    That's why we use objects with destructors.

  2. #52
    Join Date
    Nov 2004
    Beans
    2,614

    Re: Problem with Programming Threads

    Trivial to correct, you use an object internally, and wrap it's existence in the function. Then you return to syncrhonous mode in all cases, and you get correct scoping.

    Yes, that was a bug in my code. However, it's correctable. The scoping issues with your solution are not correctable, without turning your code into my code. They're a design, not an implementation flaw.

  3. #53
    Join Date
    Nov 2004
    Beans
    2,614

    Re: Problem with Programming Threads

    To be fair, that flaw does make my code near identical to yours.

    However, I still prefer my form, because unlike wrapping someting in a std::auto_ptr, TerminalControl is not inoccuous. It has potential side effects on any function call, while the only side-effect of having a std::auto_ptr wrapped object in scope longer than necessary is excessive memory utilization.

    However, it won't break your code. Having TerminalControl is scope longer than necessary will likely break your code (in non-subtle ways, non-obvious ways) so providing a non-member function to control it is worthwhile, as the scope is absolutely critical here.

Page 6 of 6 FirstFirst ... 456

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •