Boost logo

Boost :

From: Victor A. Wagner, Jr. (vawjr_at_[hidden])
Date: 2002-08-11 13:59:15


At Sunday 2002/08/11 06:48, you wrote:
>>From: "Victor A. Wagner, Jr." <vawjr_at_[hidden]>
>>At Thursday 2002/08/08 07:49, you wrote:
>>>----- Original Message -----
>>>From: "Victor A. Wagner, Jr." <vawjr_at_[hidden]>
>>> > The C++ standard is silent on the topic of thread(s)
>>>
>>>But not on the behavior of a program that has an exception thrown that would
>>>propogate out of main().
>>
>>I fail to see how an exception which is "started" by thread<whatever> can
>>be considered "inside" of main. And if it IS, then clearly I should be
>>able to write:
>>catch(...)
>>{
>>}
>>to process it
>
>Being "started by thread<whatever>" has nothing to do with it.
>
>void my_thread(boost::thread* pthread)
>{
> try
> {
> pthread->join();
> }
> catch(...)
> {
> cout << "main() threw an exception" << endl;
> }
> delete pthread;
>}
>
>int main()
>{
> boost::thread* main = new thread();
> boost::thread thrd(boost::bind(&my_thread, main));
> thread_exit(); // Yes, this is something that needs added to
> // Boost.Threads, and will be.
> throw "error!";
>}
>
>The above can't be done portably, AFAIK, and is likely to result in
>termination instead of the expected passage in join().

I have no idea what this is supposed to "do" so that it's not portable
seems irrelevant.
Although I have no idea what thread_exit() will actually do, having it
execute the next line of code seems counter-intuitive.
The following code works as I expected on VC++.NET (not that I'm attempting
to hold it out as either definitive nor conforming) and if the "if (argc >
2)" is removed or changed to, say, >1, the "unexpected exception" occurs,
otherwise this executes to completion without "problem".

#include "stdafx.h"
#include <iostream>
#include <string>
#include <iomanip>
#include <boost/lexical_cast.hpp>

using namespace std;

int main(int argc, char* argv[])
{
         cout << setw(2*argc) << ' ' << "entering main: " << argc << endl;
         if (++argc < 10)
         {
                 cout << setw(2*argc) << ' ' << "calling main: " << argc <<
endl;
                 try
                 {
                         main(argc, 0);
                 }
                 catch (exception& e)
                 {
                         cout << setw(2*argc) << ' ' << "exception: " <<
argc << " " << e.what() << endl;
                 }
                 cout << setw(2*argc) << ' ' << "returned from main: " <<
argc << endl;
         }
         if (argc > 2)
         {
                 throw exception(("throwing: " +
boost::lexical_cast<string>(argc)).c_str());
         }
         cout << setw(2*argc) << ' ' << "returning: " << argc << endl;
}

Victor A. Wagner Jr. http://rudbek.com
PGP RSA fingerprint = 4D20 EBF6 0101 B069 3817 8DBF C846 E47A
PGP D-H fingerprint = 98BC 65E3 1A19 43EC 3908 65B9 F755 E6F4 63BB 9D93
The five most dangerous words in the English language:
               "There oughta be a law"



Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk