boost::thread and local random variables

This is a discussion on boost::thread and local random variables within the C++ Programming forums, part of the General Programming Boards category; Hallo, I am trying to make my program take advantage of all the threads on my pc but im running ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485

    boost::thread and local random variables

    Hallo,

    I am trying to make my program take advantage of all the threads on my pc but im running into a strange problem. In the function used by the threads I do (float)rand() / (float)RAND_MAX and it gives the same result for every single thread.

    Any ideas on how to solve this?

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    Did you call std::srand first (and only once)?
    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
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    I did not, but adding it did not make a difference unfortunately.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    OK, I just need to check:
    You called std::srand ONCE and ONLY once in your entire program, right? Not once per thread?
    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.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    Yeah, as far as I know. I commented out as much as possible ending up with the following code, which still produces the error/bug.

    Code:
    bool RenderSingleBucket(unsigned int threadId)
    {
    	float r = Math::RandFloat();
    	float g = Math::RandFloat();
    	float b = Math::RandFloat();
    
    	return true;
    }
    
    int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
    {
    	std::srand(100);
    
    	boost::thread_group threads;
    	for(unsigned int i = 0; i < 8; i++)
    	{
    		boost::thread* t=new boost::thread(&RenderSingleBucket, i);
    		threads.add_thread(t);
    	}
    
    	threads.join_all();
    
    	return 1;
    }

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    What is this? Is Math::RandFloat some function you've made? Also, you realize that you need to call srand with a unique seed, such as the time?
    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.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    What is this?
    What do you mean?

    Is Math::RandFloat some function you've made?
    Code:
    float Math::RandFloat()
    {
    	return (float)rand() / (float)RAND_MAX;
    }

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    Quote Originally Posted by h3ro View Post
    What do you mean?
    I meant it looked like .NET. But I see that it's your own function, so that negates that issue.
    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.

  9. #9
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,659
    Looking at the implementation in the MSCRT - the static data used by srand/rand is per-thread and doesn't carry over to any newly created threads. So your srand() call in WinMain only affects the main thread (which never calls rand).

    Each of your threads needs to call srand() with a unique seed value.

    gg

  10. #10
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    That fixed it. I thought rand() might be bassed on the time/tick of the computer and that was why multiple threads executing at the same time would generate the same number. Thanks for pointing out the problem.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,589
    hmm... Codeplug made an interesting observation, but... since you are already using Boost, have you considered using Boost.Random? It has been integrated into Technical Report 1 of the extensions to the C++ standard library and, like Boost.Thread, is expected to be part of the next version of the C++ standard library.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

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