This topic is 2510 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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 ?

Thanks for any help.

##### Share on other sites

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.

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]
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.

##### Share on other sites

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

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

1. 1
2. 2
Rutin
20
3. 3
khawk
16
4. 4
A4L
14
5. 5

• 11
• 16
• 26
• 10
• 11
• ### Forum Statistics

• Total Topics
633756
• Total Posts
3013709
×