Opened 14 years ago

Closed 12 years ago

#2739 closed Bugs (wontfix)

shouldn't at_thread_exit work on the main thread?

Reported by: viboes Owned by: Anthony Williams
Milestone: Boost 1.40.0 Component: thread
Version: Boost 1.37.0 Severity: Problem
Keywords: at_thread_exit Cc:

Description

In the following example mycallable1 is not called at exit of the main thread. Shouldn't at_thread_exit work on the main thread?

    #include <boost/thread.hpp>
    #include <iostream>

    struct mycallable1
    {
        void operator()() const {
            std::cout << "mycallable1" << std::endl;
        };
    };
    struct mycallable2
    {
        void operator()() const {
            std::cout << "mycallable2" << std::endl;
        };
    };

    void my_thread() {
        std::cout << "my_thread" << std::endl;
        mycallable1 x;
        boost::this_thread::at_thread_exit(x);
    }

    int main() {
        mycallable2 x;
        boost::this_thread::at_thread_exit(x);

        boost::thread th(my_thread);
        th.join();
        return 0;
    }

Output: my_thread mycallable1

Attachments (2)

test_ticket_2739.cpp (1.3 KB ) - added by viboes 13 years ago.
2739.patch (1001 bytes ) - added by viboes 13 years ago.

Download all attachments as: .zip

Change History (8)

comment:1 by viboes, 13 years ago

Milestone: Boost 1.39.0Boost 1.40.0

The attached files contains the tests showing the bug and a patch solving the issue. The idea been use atexit for the main thread.

by viboes, 13 years ago

Attachment: test_ticket_2739.cpp added

by viboes, 13 years ago

Attachment: 2739.patch added

comment:2 by viboes, 13 years ago

I have no modified nothing for window as I can not check it. Can someone apply the same modifications to the src/win32/thread.cpp file?

in reply to:  1 comment:3 by viboes, 13 years ago

Replying to viboes:

The attached files contains the tests showing the bug and a patch solving the issue. The idea been use atexit for the main thread.

I have run the Thread tests and the patch introduce a regression on test_tss.cpp:test_tss. The reason is that the patch do not make a difference between the thread main and native threads created directly with pthread_create. So the patch is not usable. Is there a way to identify the thread main?

comment:4 by Vladimir Prus, 13 years ago

Severity: ShowstopperProblem

It's not apparent that this is showstopper bug, nor even regression.

comment:5 by cowwoc@…, 13 years ago

Hi,

I can confirm that this issue affects Boost 1.40 under Windows. Please fix it for this platform too!

comment:6 by Anthony Williams, 12 years ago

Resolution: wontfix
Status: newclosed

No, at_thread_exit should not work on the main thread.

If you call exit() then all threads are abruptly terminated without calling their exit handlers.

Returning from main is defined to be equivalent to calling exit(), so all threads (including the main thread) are terminated without calling exit handlers.

On POSIX, if you exit the main thread by calling pthread_exit() then the thread exit handlers should run. Similarly, on Windows you can use ExitThread(). However, I would recommend using atexit or explicit calls, or even just normal destructors, since using pthread_exit() or ExitThread will not run destructors for automatic_objects.

Note: See TracTickets for help on using tickets.