Thread: boost::thread and local random variables

  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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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,981
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed