CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Apr 2009
    Posts
    38

    Accessing array in a thread (boost)

    I was wondering if it was possible to access data from an array inside a threaded function after it has finished executing?

    Take this example:

    Code:
    #include <iostream>
    #include <boost/thread.hpp>
    
    using namespace std;
    
    void workerFunc(int start){
    	int arr[5];
    	int i;
    
    	for(i = 0; i < 5; i++){
    		arr[i] = start;
    		start++;
    	}
    
    }
    
    int main(){
        boost::thread workerThread(workerFunc, 3);
        workerThread.join();
    
        return 0;
    }
    After workerFunc has finished, how could I access the array arr? Is there any way to get (3,4,5,6,7) back into main()?

    Thank You

  2. #2
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Accessing array in a thread (boost)

    you can't, it is gone when the workerFunc() exits.

    if you need access to it from outside the thread, either make the array global, or make it local to the thread that starts the worker thread and pass it aloong into the worker thread. If you can guarantee that your main thread won't ever access the array until the worker thread is finished, that's all you need.
    If your main thread needs to access the array while the thread is running, you will need to provide proper synchronization.

  3. #3
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Accessing array in a thread (boost)

    Allocate the array in main instead and pass a pointer/reference to it to the workerFunc
    Last edited by S_M_A; November 22nd, 2012 at 12:06 PM.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  4. #4
    Join Date
    Apr 2009
    Posts
    38

    Re: Accessing array in a thread (boost)

    Thank you, I didn't realise it was that straight forward!

    Here is the code in case anyone else needs to do something similar:

    Code:
    #include <iostream>
    #include <boost/thread.hpp>
    
    using namespace std;
    
    void workerFunc(int start, int a[5]){
    	int i;
    
    	for(i = 0; i < 5; i++){
    		a[i] = start;
    		start++;
    	}
    
    }
    
    int main(){
    	int arr[5];
        boost::thread workerThread(workerFunc, 3, arr);
        workerThread.join();
    	cout << arr[2];
    	system("pause");
    
        return 0;
    }

  5. #5
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Accessing array in a thread (boost)

    Another possibility is to use a future to return a value from the thread function. This will not work with a static array, because you cannot return a static array from a function, but it will work with pretty much anything else.
    See the example in the boost documentation: http://www.boost.org/doc/libs/1_52_0...tures.creating
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Accessing array in a thread (boost)

    Quote Originally Posted by SamstaUK View Post
    Thank you, I didn't realise it was that straight forward!

    Here is the code in case anyone else needs to do something similar:
    The flaw in your function is that anyone can pass a smaller array, and that function would not detect it.
    Code:
    void workerFunc(int start, int a[5]){
    That definition does not ensure that the array is of 5 elements. In C++, arrays decay to pointers. The definition above is the same as this:
    Code:
    void workerFunc(int start, int* a)
    If I passed an array of 2 elements instead of 5, your function would still go through and process 5 items, causing undefined behaviour. This is the flaw of using "simple and dumb" arrays -- there is no bounds checking. Instead, proper usage of container classes (i.e. std::vector<int>) would be appropriate.

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured