Thread: Using std::future to manipulate objects in C++11

  1. #1
    Registered User
    Join Date
    Nov 2013
    Posts
    2

    Using std::future to manipulate objects in C++11

    I'm experimenting with std::future, but haven't found any materials that deal with the usecase I have in mind and was hoping someone could either confirm or deny the validity of the approach excerpted below:


    Code:
        class Thing {
        private:
        	
        	std::string mValue;
        	
        public:
        	Thing() : mValue( "" ) {}
        	
        	void set(const std::string& s)
        	{
        		mValue = s;
        		reverse( mValue.begin(), mValue.end() );
        	}
        	
        	const std::string& getValue() const
        	{
        		return mValue;
        	}
        };
        
        int main(int argc, const char * argv[])
        {
        	Thing* tA = new Thing();
        	Thing* tB = new Thing();
        	Thing* tC = new Thing();
        	
        	auto tLambda = [](const string& iStr, Thing* oThing) -> Thing* {
        		oThing->set( iStr );
        		return oThing;
        	};
        	
        	future<Thing*> tA1 = async( launch::async, tLambda, "This is a test...A", tA );
        	future<Thing*> tB1 = async( launch::async, tLambda, "This is a test...B", tB );
        	future<Thing*> tC1 = async( launch::async, tLambda, "This is a test...C", tC );
        	
        	future<Thing*> tA2 = async( launch::async, tLambda, tA1.get()->getValue(), tA );
        	future<Thing*> tB2 = async( launch::async, tLambda, tB1.get()->getValue(), tB );
        	future<Thing*> tC2 = async( launch::async, tLambda, tC1.get()->getValue(), tC );
        	
        	cout << tA2.get()->getValue() << endl;
        	cout << tB2.get()->getValue() << endl;
        	cout << tC2.get()->getValue() << endl;
        	
            return 0;
        }
    In essence, I'd like to use std::future to update a pre-existing object's member variables rather than initializing and returning a new object (as is shown in most std::future examples I've seen). In the example above, the model seems a little suspect. I would love to hear any thoughts on the best way to achieve this sort of functionality. Ultimately, I would like to embed this in a thread-pool sort of context.


    Thanks!

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The point of a future is to for a thread to compute and return a result. Modifying a member variable is not what a future is for, I would think, and as such I would avoid doing so.
    Why do you need to update a member variable in such a fashion?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    On line 36-38, you don't want to call get in the main thread. You should call it inside the lambda, meaning these lines should have their own lambda. Pass the future to the lambda instead of Thing *.

    There are better facilities planned for this kind of thing in future versions of C++. But with the above change, I think this is a reasonable thing to do this way if you're sticking to current standard libraries.

    Similarly, on the same lines it would be better to use the pointer from tX1.get() instead of the original value. You're ok in this example, but I could see more complex cases where what you have would lead to a race condition.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by Elysia View Post
    The point of a future is to for a thread to compute and return a result. Modifying a member variable is not what a future is for, I would think, and as such I would avoid doing so.
    Why do you need to update a member variable in such a fashion?
    Presumably because an in place modification of the result is more efficient than passing copies around between threads.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    Registered User
    Join Date
    Nov 2013
    Posts
    2
    Quote Originally Posted by King Mir View Post
    Presumably because an in place modification of the result is more efficient than passing copies around between threads.
    Yes, this was my thinking exactly. Though, naturally I see the surrounding danger in this approach. Thanks to both of you for your thoughts. It seems like this approach works in limited circumstances, but should probably be rethought for more general applicability and safety.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 05-24-2011, 06:36 PM
  2. Can't manipulate variable!
    By smithc2005 in forum C Programming
    Replies: 2
    Last Post: 10-22-2008, 07:18 AM
  3. Manipulate bytes
    By davide_82 in forum C Programming
    Replies: 8
    Last Post: 11-02-2005, 03:46 AM
  4. Replies: 4
    Last Post: 10-16-2003, 11:26 AM
  5. How to manipulate a string
    By Basia in forum C Programming
    Replies: 4
    Last Post: 09-29-2002, 09:35 PM

Tags for this Thread