Thread: Why does this throw an std::system_error ?

  1. #1
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657

    Why does this throw an std::system_error ?

    I was playing around with threads to improve some code.....with no luck whatsoever..
    Any way.. I tried to get the following loop running concurrently.
    Code:
                for(auto x=args.begin();x!=args.end();x++)
                {
                    list l;
                    if(is_list(*x,l)) *x = l.eval();
                }
    My try:
    Code:
                std::vector<std::thread> threads;
                list temp;
                for(auto x=args.begin();x!=args.end();x++)
                    if(is_list(*x,temp))
                        threads.push_back(std::thread([](list l,std::string& ret){ret = l.eval();},temp,*x));
                for(auto t = threads.begin();t!=threads.end();t++)t->join();
    (Notice that the list is passed by value...to avoid mishaps)
    But clearly something(else?) has gone wrong...

    N.B: list is NOT std::list
    Last edited by manasij7479; 01-03-2012 at 07:53 PM.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    What is the exception and what object raised it?

    Also the very fact that you need to remind people that list is not std::list is disturbing. Treat list like a reserved word.

  3. #3
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by whiteflags View Post
    What is the exception and what object raised it?

    Also the very fact that you need to remind people that list is not std::list is disturbing. Treat list like a reserved word.
    I'm not sure... but it seems to stop when the 2nd thread is about to be created.
    Quote Originally Posted by gdb
    (gdb)
    Continuing.
    terminate called after throwing an instance of 'std::system_error'
    what(): Operation not permitted


    Program received signal SIGABRT, Aborted.
    0x00133416 in __kernel_vsyscall ()
    (gdb)

  4. #4
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    The problem was a silly one.... I forgot to give the pthread flag to gcc.(..and another small bug in the loop itself)
    Here is the final code:
    Code:
    			std::vector<std::thread> threads;
    			for(auto x=args.begin();x!=args.end();x++)
    			{
    				threads.push_back
    				(
    					std::thread
    					(
    						[](list::iterator li)
    						{
    							list l;
    							if(is_list(*li,l))
    								*li = l.eval();
    						},
    						x
    					)
    				);
    			}
    			for(auto& t:threads)t.join();

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Throw away the key!
    By Salem in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 06-12-2008, 08:28 PM
  2. Why does C++ need throw()
    By meili100 in forum C++ Programming
    Replies: 19
    Last Post: 11-10-2007, 12:34 PM
  3. function throw
    By l2u in forum C++ Programming
    Replies: 3
    Last Post: 04-30-2007, 08:09 AM
  4. Try...catch...throw or ifs?
    By Kylecito in forum C++ Programming
    Replies: 9
    Last Post: 03-02-2006, 10:41 PM
  5. re-throw an exception
    By filler_bunny in forum C++ Programming
    Replies: 7
    Last Post: 10-15-2003, 06:06 AM