Click here to Skip to main content
15,888,031 members

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrassing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
GeneralWTF! PinPopular
leppie17-Aug-10 22:03
leppie17-Aug-10 22:03 
GeneralRe: WTF! PinPopular
Richard A. Dalton18-Aug-10 0:05
Richard A. Dalton18-Aug-10 0:05 
GeneralRe: WTF! Pin
OriginalGriff18-Aug-10 0:51
mveOriginalGriff18-Aug-10 0:51 
GeneralRe: WTF! Pin
Le centriste19-Aug-10 2:16
Le centriste19-Aug-10 2:16 
GeneralRe: WTF! Pin
BillW3325-Aug-10 6:37
professionalBillW3325-Aug-10 6:37 
JokeRe: WTF! Pin
akyriako7820-Aug-10 2:57
akyriako7820-Aug-10 2:57 
GeneralRe: WTF! Pin
CDP180220-Aug-10 4:35
CDP180220-Aug-10 4:35 
QuestionCargo cult programming for thread safe variable access? Pin
User 58261916-Aug-10 2:28
User 58261916-Aug-10 2:28 
I found the following class while debugging an application showing infrequent deadlocks:

template <class T>
class Protector
{
private:
   CRITICAL_SECTION CS;
   T data;

public:
   Protector() { InitializeCriticalSection(&CS); };
  ~Protector() { DeleteCriticalSection(&CS); };

   void operator=(const T& value)
   {
     EnterCriticalSection(&CS);
     try { data = value; } catch(...){}
     LeaveCriticalSection(&CS);
   };


   T& __fastcall operator *()
   {
      EnterCriticalSection(&CS);
      T &value = data;
      LeaveCriticalSection(&CS);
      return value;
   }

   T* __fastcall operator ->()
   {
     return &data;
   }

   __fastcall operator T()
   {
      EnterCriticalSection(&CS);
      T& value = data;
      LeaveCriticalSection(&CS);
      return value;
   }
};


The class is used to wrap member variables supposedly for adding thread safety to them. For instance:

class foo
{
  Protector<double> m_foobar;
}


It's something like an interlocked exchange based on RAII except for it doesnt make much sense. For me this appears to be cargo cult programming that does not bring any thread safety at all. My main concern are these lines:

EnterCriticalSection(&CS);
T& value = data;
LeaveCriticalSection(&CS);
return value;


This appears to be pure nonsense since the lock is acquired just in order to assign the a reference to data. (what on earth could change the address of data while in here?) The lock is then immediately unlocked and the reference returned. What is this supposed to protect? The reference can never change since &data can not change and the value in data is not protected since the lock is released immediately.

I think i'm going to get rid of this since it does'nt appear to serve any real purpose. Any other thoughts on that?

AnswerRe: Cargo cult programming for thread safe variable access? Pin
Luc Pattyn16-Aug-10 3:03
sitebuilderLuc Pattyn16-Aug-10 3:03 
GeneralRe: Cargo cult programming for thread safe variable access? Pin
User 58261916-Aug-10 4:13
User 58261916-Aug-10 4:13 
GeneralRe: Cargo cult programming for thread safe variable access? Pin
Luc Pattyn16-Aug-10 4:26
sitebuilderLuc Pattyn16-Aug-10 4:26 
GeneralRe: Cargo cult programming for thread safe variable access? Pin
User 58261916-Aug-10 5:05
User 58261916-Aug-10 5:05 
GeneralRe: Cargo cult programming for thread safe variable access? Pin
Luc Pattyn16-Aug-10 5:23
sitebuilderLuc Pattyn16-Aug-10 5:23 
AnswerRe: Cargo cult programming for thread safe variable access? Pin
dojohansen16-Aug-10 3:09
dojohansen16-Aug-10 3:09 
GeneralRe: Cargo cult programming for thread safe variable access? Pin
User 58261916-Aug-10 4:35
User 58261916-Aug-10 4:35 
AnswerRe: Cargo cult programming for thread safe variable access? Pin
Mladen Janković16-Aug-10 6:13
Mladen Janković16-Aug-10 6:13 
AnswerRe: Cargo cult programming for thread safe variable access? Pin
ed welch19-Aug-10 4:38
ed welch19-Aug-10 4:38 
GeneralUh...check what? PinPopular
TheyCallMeMrJames12-Aug-10 6:34
TheyCallMeMrJames12-Aug-10 6:34 
GeneralRe: Uh...check what? PinPopular
OriginalGriff12-Aug-10 8:21
mveOriginalGriff12-Aug-10 8:21 
GeneralRe: Uh...check what? Pin
GibbleCH12-Aug-10 8:51
GibbleCH12-Aug-10 8:51 
GeneralRe: Uh...check what? Pin
Andre xxxxxxx13-Aug-10 0:50
Andre xxxxxxx13-Aug-10 0:50 
GeneralRe: Uh...check what? Pin
Super Lloyd15-Aug-10 15:20
Super Lloyd15-Aug-10 15:20 
GeneralRe: Uh...check what? Pin
Andre xxxxxxx16-Aug-10 0:51
Andre xxxxxxx16-Aug-10 0:51 
GeneralRe: Uh...check what? PinPopular
Ian Shlasko12-Aug-10 9:40
Ian Shlasko12-Aug-10 9:40 
GeneralRe: Uh...check what? Pin
Bigdeak12-Aug-10 19:42
Bigdeak12-Aug-10 19:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.