• Create Account

### #ActualvNeeki

Posted 03 February 2012 - 01:16 AM

A.What do you think of AddRef ? Shouldn't i suspend the thread at that time ? If so how would i do it ?

AddRef is not thread safe. Pre C++0x versions of C++ lack a standard threading model, so you cannot rely on writes being either atomic or immediately published to all cores.processors.

A minimal change would be to introduce a global mutex, which is locked in before any thread reads or writes this data, and unlocked when the reading/writing completes. Such locking will effectively serialize access to that code. Unless you introduce fine grained locking, your overall program acts in a serial fashion, and your multi-threaded solution can actually become slower than a single-threaded implementation.

A better solution would be to re-architect the problem in a thread-friendly manner. For example, the producer/consumer pattern. Thread friendly solutions attempt to minimise the frequency and duration of synchronisation events, such as locking.

The key to high performance threading is to totally minimise the amount of communication between threads. Even having two different variables written in different threads can cause performance issues, due to large cache lines and false sharing.

Thanks for sharing the knowledge.I think i will pick your local buffer approach which sounds very easy to implement.

It isn't documented as being thread safe. Indeed, looking at the SDL 1.2.14 source code, there appears to be a race condition in the first call to SDL_GetTicks() on macos platforms.

Got it!
I will make sure to call SDL_GetTicks() just before the thread gets spawned.

You didn't ask this, but you should't call SDL_KillThread(). Killing a thread can cause deadlocks. If thread A kills thread B, and thread B holds a lock that thread A later tries to acquire, your program will fail to make progress and the user will likely be forced to manually intervene to halt the process.

A better solution is co-operative shut down. For example thread periodically polls some kind of thread safe shut down flag. The "master" thread sets this flag, and waits some multiple of the polling interval until all threads have exited. If the threads are taking too long, then you might think about killing them. However, it is usually easier to just ask the O.S. to terminate the process, as that will avoid race conditions and bugs in your kill logic which could likewise hang the process.

Thanks for all these priceless hints , i will start implementing everything from scratch a-s-a-p.

++++++++rep; + bookmarked.


### #1vNeeki

Posted 03 February 2012 - 01:12 AM

A.What do you think of AddRef ? Shouldn't i suspend the thread at that time ? If so how would i do it ?

AddRef is not thread safe. Pre C++0x versions of C++ lack a standard threading model, so you cannot rely on writes being either atomic or immediately published to all cores.processors.

A minimal change would be to introduce a global mutex, which is locked in before any thread reads or writes this data, and unlocked when the reading/writing completes. Such locking will effectively serialize access to that code. Unless you introduce fine grained locking, your overall program acts in a serial fashion, and your multi-threaded solution can actually become slower than a single-threaded implementation.

A better solution would be to re-architect the problem in a thread-friendly manner. For example, the producer/consumer pattern. Thread friendly solutions attempt to minimise the frequency and duration of synchronisation events, such as locking.

The key to high performance threading is to totally minimise the amount of communication between threads. Even having two different variables written in different threads can cause performance issues, due to large cache lines and false sharing.

Thanks for sharing the knowledge.I think i will pick your local buffer approach which sounds very easy to implement.

It isn't documented as being thread safe. Indeed, looking at the SDL 1.2.14 source code, there appears to be a race condition in the first call to SDL_GetTicks() on macos platforms.

Got it!
I will make sure to call SDL_GetTicks() just before the thread gets spawned.

You didn't ask this, but you should't call SDL_KillThread(). Killing a thread can cause deadlocks. If thread A kills thread B, and thread B holds a lock that thread A later tries to acquire, your program will fail to make progress and the user will likely be forced to manually intervene to halt the process.

A better solution is co-operative shut down. For example thread periodically polls some kind of thread safe shut down flag. The "master" thread sets this flag, and waits some multiple of the polling interval until all threads have exited. If the threads are taking too long, then you might think about killing them. However, it is usually easier to just ask the O.S. to terminate the process, as that will avoid race conditions and bugs in your kill logic which could likewise hang the process.

Thanks for all these priceless hints , i will start implementing everything from scratch a-s-a-p.

++++++++rep;


PARTNERS