Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - vechestva

Pages: 1 [2]
16
System / Re: multithreading and srand
« on: December 10, 2012, 08:44:55 am »
binary1248, thank you. Thanks to all.

17
System / Re: multithreading and srand
« on: December 09, 2012, 02:13:20 pm »
You didn't show us how you tried to use the mutex, and didn't say how it failed.
class Myc
{
public:
        Myc()
        {
        }

        ~Myc()
        {
                for (int i = 0; i < m_threads.size(); ++i)
                        if(sf::Thread* thr = m_threads[i])
                        {
                                thr->terminate();
                                delete thr;
                        }
                m_threads.clear();
        }

        void Start()
        {
                // 3 thread
                for (int i = 0; i < 3; ++i)
                        m_threads.push_back(new sf::Thread(&Myc::ThrFun, this));

                for (int i = 0; i < m_threads.size(); ++i)
                        m_threads[i]->launch();
        }

        void ThrFun()
        {
                sf::Clock clock;

                while (true)
                {
                        if (clock.getElapsedTime().asSeconds() >= 3)
                        {
                                // problem
                                m_mutex.lock();
                                int r = rand()%100+1;
                                m_mutex.unlock();
                                std::cout << r << std::endl;

                                clock.restart();
                        }
                }
        }

private:
        std::vector<sf::Thread*> m_threads;
        sf::Mutex m_mutex;
};

int main()
{
        srand((unsigned)time(NULL));

        Myc m;
        m.Start();

        std::cin.get();
}

18
System / Re: multithreading and srand
« on: December 08, 2012, 01:50:46 pm »
thx, but:
tons
This is the key word. ;)

The alternative is to protect global rand() calls with a mutex. But this can quickly become slow. An own random engine per thread is much cleaner. And you can enforce deterministic generation of random numbers, which may be helpful for debugging.
Mutex also fails. :'(
Since the mutex does not work, or I did not use true.

19
System / Re: multithreading and srand
« on: December 08, 2012, 10:26:12 am »

20
System / Re: multithreading and srand
« on: December 08, 2012, 05:22:36 am »

21
System / Re: multithreading and srand
« on: December 08, 2012, 04:32:04 am »
I have visual studio 2008. There is no support <random> and rand_r (drand48_r). How to solve the problem? (change "IDE" is not an option).

22
System / multithreading and srand
« on: December 07, 2012, 01:58:46 pm »
Hi.  :)
How to use a randomizer with multithreading?
simple ex:
class Myc
{
public:
        Myc()
        {
        }

        ~Myc()
        {
                for (int i = 0; i < threads.size(); ++i)
                        if(sf::Thread* thr = threads[i])
                        {
                                thr->terminate();
                                delete thr;
                        }
                threads.clear();
        }

        void Start()
        {
                // 3 thread
                for (int i = 0; i < 3; ++i)
                        threads.push_back(new sf::Thread(&Myc::ThrFun, this));

                for (int i = 0; i < threads.size(); ++i)
                        threads[i]->launch();
        }

        void ThrFun()
        {
                sf::Clock clock;

                while (true)
                {
                        if (clock.getElapsedTime().asSeconds() >= 3)
                        {
                                // problem
                                int r = rand()%100+1;
                                std::cout << r << std::endl;

                                clock.restart();
                        }
                }
        }

private:
        std::vector<sf::Thread*> threads;
};

int main()
{
        srand((unsigned)time(NULL));

        Myc m;
        m.Start();

        std::cin.get();
}
 

result:
The same value in each thread. Since the mutex does not work, or I did not use true.

23
Graphics / Re: Divide the image into parts for animation.
« on: October 05, 2012, 12:25:20 pm »
Can I set the original "textureRect", without creating an instance?
so I do not like::
sf::IntRect origRect = spite.getTextureRect();
sprite.SetTextureRect(0, 0, 50, 50);

/*
    ....
*/


// return to original
sprite.SetTextureRect(origRect);
 

24
Graphics / Re: Divide the image into parts for animation.
« on: October 03, 2012, 03:30:56 pm »

25
Graphics / Divide the image into parts for animation.
« on: October 03, 2012, 03:16:49 pm »
Hello! How to divide the image to animate?
Give an example, please.

img example source:


Pages: 1 [2]