• Create Account

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

2 replies to this topic

### #1vNeeki  Members   -  Reputation: 194

Like
0Likes
Like

Posted 02 February 2012 - 12:09 AM

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[i])
data[i]->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[i] = 0;

return true;
}

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

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

for (int i = 0;i < data_ptr;i++)
{
if (!data[i])
{
data[i] = 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 ?

Thanks for any help.

### #2rip-off  Moderators   -  Reputation: 10497

Like
1Likes
Like

Posted 02 February 2012 - 05:52 PM

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.

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.

### #3vNeeki  Members   -  Reputation: 194

Like
0Likes
Like

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; + bookmarked.


Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS