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.What do you think of AddRef ? Shouldn't i suspend the thread at that time ? If so how would i do it ?
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.
There are two broad, common approaches. The first is to aggressively copy any data that needs to be shared between threads. The owner thread builds a copy of the data required for a given task, and then the worker thread(s) gets exclusive write or shared read access to this data. Another is hand-off, the owner thread reserves some memory for the task, and gives the worker thread(s) exclusive write access to the memory. While the task is ongoing, the owner does not access the task data (save perhaps polling some thread-safe status or result queue). When the worker completes, it publishes its result somewhere and relinquishes access to the task memory.
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.
B.Is SDL_GetTicks() thread safe ?
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.