Threading question

Started by
1 comment, last by vNeeki 12 years, 2 months ago
Hello!

I'm using threads for the first time , and im making use of SDL's threading functions which are easy and i've got everything working perfectly...or at least that's what i believe , which is what gave me the motivation to ask if im doing everything properly.



This is a sample unit test which simulates the behaviour of the task by 99,9% :



SomeDynamicBlock** data;
int data_ptr,data_len;

int WorkWorkWork(void* data)
{
unsigned int oldTime = 0;

while (true)
{
if (SDL_GetTicks() - oldTime > 1000)
{
for (int i = 0,j = data_ptr;i < j;i++)
{
if (data)
data->Update(SDL_GetTicks());
}
oldTime = SDL_GetTicks();
}
}
return 0;
}

bool SetupWorker(int size)
{
//Only happens once!
data = new SomeDynamicBlock*[size];
data_ptr = 0;
data_len = size;

for (int i = 0;i < data_len;i++)
data = 0;

sdl_work_thread = CreateThread(WorkWorkWork);
return true;
}

void KillWorker()
{
SDL_KillThread(sdl_work_thread);
delete data;
data = 0;
}

bool AddRef(SomeDynamicBlock* shared)
{
if (data_ptr < data_len)
{
data[data_ptr++] = shared;
return true;
}

for (int i = 0;i < data_ptr;i++)
{
if (!data)
{
data = shared;
return true;
}
}
return false;
}



And these are my questions :

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

Thanks for any help.
Advertisement

A.What do you think of AddRef ? Shouldn't i suspend the thread at that time ? If so how would i do it ?
[/quote]
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.

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.


B.Is SDL_GetTicks() thread safe ?
[/quote]
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.

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.


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.

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.
[/quote]

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




B.Is SDL_GetTicks() thread safe ?

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.
[/quote]

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.

This topic is closed to new replies.

Advertisement