Why does this throw an std::system_error ?

This is a discussion on Why does this throw an std::system_error ? within the C++ Programming forums, part of the General Programming Boards category; I was playing around with threads to improve some code.....with no luck whatsoever.. Any way.. I tried to get the ...

  1. #1
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,490

    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 06:53 PM.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,532
    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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,490
    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)
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  4. #4
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,490
    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();
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



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, 11:34 AM
  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, 09:41 PM
  5. re-throw an exception
    By filler_bunny in forum C++ Programming
    Replies: 7
    Last Post: 10-15-2003, 06:06 AM

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