Jump to content
  • Advertisement
Sign in to follow this  
MassacrerAL

One writer many readers

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello everyone, is it ok to have one writing thread and many reading threads working on the same memory? I intend to do renderer in separate thread and I`m looking for the best way of one way thread communication, app logic --> renderer. Is there some good waz to do this?

Share this post


Link to post
Share on other sites
Advertisement
One writer and many readers for what? It's probably safe for a single int. It's definitely not safe for a traditional doubly linked list unless you have synchronization in place.

Share this post


Link to post
Share on other sites
It's also not safe for certain containers, like a que where elements are read on one end, then written to the other.

But, you can always use locks, critical sections on Windows, to be safe and not worry about it.

The Google keywords are "thread safety", "atomic", and "locks".

Share this post


Link to post
Share on other sites
Quote:
Original post by MassacrerAL
is it ok to have one writing thread and many reading threads working on the same memory?

Generally not, unless you can guarantee atomicity of the write and the reads. Which you can usually not.

Imagine a reading thread accessing the data. It has half done reading it, when suddenly the writing thread modifies it. Bam, corrupted data.

Many concurrent reading threads alone are okay. But as soon as you write to the data, you need sync. Either through atomic operations (look into lockfree algorithms) or through the usual synchronization primitives (mutex, critical sections, etc).

Share this post


Link to post
Share on other sites
If you are not sure if resource is thread-safe, it is fairly simple to implement an interface for it.

class{
private:
// It is possible to have separated indicator for each resource you are using
bool locked;
resource res;
something use_resource();
public:
// This function forces thread to wait until resource is free.
function(){
while(locked){ sleep(); }
locked = true;
something x = use_resource();
locked = false;
return x;
}
// This function will return instantly if resource is being used.
// User should cache the results and if fail is returned, previous results can be used instead.
approach_2(){
if(locked){
return fail;
}
locked = true;
something x = use_resource();
locked = false;
return x;
}
}



Share this post


Link to post
Share on other sites
Quote:
Original post by Splinter of Chaos
But, you can always use locks, critical sections on Windows, to be safe and not worry about it.
The Google keywords are "thread safety", "atomic", and "locks".
In general, just locking data for the hell of it is a very bad idea. Better to determine fully what you are trying to accomplish, as you may find you can do without locks (for instance, using atomic ring buffers), or you may discover that naive locking schemes are very inefficient.

Share this post


Link to post
Share on other sites
Your approach seems okay, just make sure you get the synchronization correct. Your renderer thread could use a work queue that sould be synchronized with a reader-writer or producer-consumer type of synchronization.

Quote:
Original post by ville-v
If you are not sure if resource is thread-safe, it is fairly simple to implement an interface for it.

*** Source Snippet Removed ***


This is how you should not do it. The locking does not work, because the test-and-set operation is not atomic (while(locked) { wait(); }; locked = true;). It is trivial to find a case where more than one thread can enter the critical section.

Instead of this kind of locking mechanism, you should look at synchronization primitives such as mutexes and condition variables that are provided by your operating system (using interrupt disabling and cpu-level atomic operations and low level primitives such as semaphores and spinlocks). They can be used to provide robust synchronization mechanisms. You should also try to use (variations of) known parallel programming school book solutions such as readers-writers, producer-consumer, monitor, etc whenever they can be applied to your solution.

Parallel programming is a huge paradigm shift from single threaded coding. You can't use your single-thread tricks as you used to.

-Riku

Share this post


Link to post
Share on other sites
Quote:
Original post by ville-v
If you are not sure if resource is thread-safe, it is fairly simple to implement an interface for it.

*** Source Snippet Removed ***


Neither of those two approaches are thread-safe. The bool variable isn't even marked volatile, so the code compiler may produce can be just about anything.

Quote:
is it ok to have one writing thread and many reading threads working on the same memory?


Yes, arbitrary number of threads can access same memory address concurrently.
No, the results might not make sense if contents of such address are modified - that's up to user to ensure.

Quote:
I intend to do renderer in separate thread and I`m looking for the best way of one way thread communication, app logic --> renderer. Is there some good waz to do this?


Since renderer typically needs to run in single thread, wouldn't that be single-writer-single-reader or multiple-writers-single-reader?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!