Click here to Skip to main content
15,892,537 members
Articles / Programming Languages / C++

A C++ Cross-Platform Thread Class

Rate me:
Please Sign up or sign in to vote.
4.56/5 (26 votes)
15 Nov 20034 min read 518.7K   1.8K   49  
Write portable object-oriented threads that work on Win32 and Posix-Compliant systems without modification.
/////////////////////////////////////////////////////////////////////
//  Written by Phillip Sitbon
//  Copyright 2003
//
//  Win32/Mutex.h
//    - Resource locking mechanism using Critical Sections
//
/////////////////////////////////////////////////////////////////////

#ifndef _Mutex_Win32_
#define _Mutex_Win32_

#include "Win32.h"

class Mutex
{
  mutable CRITICAL_SECTION C;
  void operator=(Mutex &M) {}
  Mutex( const Mutex &M ) {}

  public:

  Mutex()
  { InitializeCriticalSection(&C); }

  virtual ~Mutex()
  { DeleteCriticalSection(&C); }

  int Lock() const
  { EnterCriticalSection(&C); return 0; }

#if(_WIN32_WINNT >= 0x0400)
  int Lock_Try() const
  { return (TryEnterCriticalSection(&C)?0:EBUSY); }
#endif

  int Unlock() const
  { LeaveCriticalSection(&C); return 0; }
};

#endif // !_Mutex_Win32_

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
I am a student at Portland State University in Portland, Oregon majoring in Mathematics and minoring in Computer Science and Physics.

Comments and Discussions