views:

163

answers:

4

Is it ok to check the current thread inside a function?

For example if some non-thread safe data structure is only altered by one thread, and there is a function which is called by multiple threads, it would be useful to have separate code paths depending on the current thread. If the current thread is the one that alters the data structure, it is ok to alter the data structure directly in the function. However, if the current thread is some other thread, the actual altering would have to be delayed, so that it is performed when it is safe to perform the operation.

Or, would it be better to use some boolean which is given as a parameter to the function to separate the different code paths?

Or do something totally different?

What do you think?

+6  A: 

You are not making all too much sense. You said a non-thread safe data structure is only ever altered by one thread, but in the next sentence you talk about delaying any changes made to that data structure by other threads. Make up your mind.

In general, I'd suggest wrapping the access to the data structure up with a critical section, or mutex.

Jim Brissom
Yes, it must be delayed because it cannot be altered by any other thread.
qevin
@qevin: then you need thread synchronization. simple as that. dont try to trick mother nature. she'll turn you in to a frog
John Dibling
+1, d'oh I answered before reading that part. Yeah, either a resource is shared or it is not. Binary decision. There's no "it will be shared later," it is shared for the entire time but the owner just happens to be known up to a certain point.
Potatoswatter
A: 

It's possible to use such animals as reader/writer locks to differentiate between readers and writers of datastructures but the performance advantage for typical cases usually wont merit the additional complexity associated with their use.

From the way your question is stated, I'm guessing you're fairly new to multithreaded development. I highly suggest sticking with the simplist and most commonly used approaches for ensuring data integrity (most books/articles you readon the issue will mention the same uses for mutexes/critical sections). Multithreaded development is extremely easy to get wrong and can be difficult to debug. Also, what seems like the "optimal" solution very often doesn't buy you the huge performance benefit you might think. It's usually best to implement the simplist approach that will work then worry about optimizing it after the fact.

Rakis
It would be stupid to use mutex when the data stucture is 99.999% of the cases altered by the main thread, and very rarely altered by some other thread.
qevin
And yes, I'm in very performance critical environment.
qevin
@qevin - it would be even more stupid to not use a mutex and ship your code with obscure concurrency bugs. If you have multiple threads writing the same data then some kind of mutex is going to be necessary. If you can modify your design to have ALL writes done on your main thread, great. Perhaps the other threads could package and hand off their updates for the main thread to actually do the work.
Steve Townsend
@qevin: everyone and their brother thinks their code is somehow special. Have you actually tested using critical sections to see what the performance impact would be? For example, a Windows CRITICAL_SECTION is quite fast.
John Dibling
At these times, you wish OS vendors simply had a `#define FAST_MUTEX Mutex` in their headers, so you could point people with "very performance critical environments" to that. It's not performance-criticial if you can't quote figures!
MSalters
A: 

There is a trick that could work in case, as you said, the other threads will only make changes only once in a while, although it is still rather hackish:

  • make sure your "master" thread can't be interrupted by the other ones (higher priority, non fair scheduling)
  • check your thread
  • if "master", just change
  • if other, put off scheduling, if needed by putting off interrupts, make change, reinstall scheduling
  • really test to see whether there are no issues in your setup.

As you can see, if requirements change a little bit, this could turn out worse than using normal locks.

stefaanv
A: 

As mentioned, the simplest solution when two threads need access to the same data is to use some synchronization mechanism (i.e. critical section or mutex).

If you already have synchronization in your design try to reuse it (if possible) instead of adding more. For example, if the main thread receives its work from a synchronized queue you might be able to have thread 2 queue the data structure update. The main thread will pick up the request and can update it without additional synchronization.

The queuing concept can be hidden from the rest of the design through the Active Object pattern. The activ object may also be able to publish the data structure changes through the Observer pattern to other interested threads.

skimobear