Menu

- Learn
- Documentation
  - Language Reference
  - Library Reference
  - Feature Overview
  - DMD Manual
  - Articles
- Packages
- Community
  - Bug Tracker
  - Forums
  - IRC
  - D on GitHub
  - Wiki
  - Review Queue
  - Twitter
  - More Links
- Resources
  - NEW Library Reference Preview
  - D-Specific Tools
  - Visual D
  - Editors
  - IDEs
  - Tutorials
  - Books
  - The D Style
  - Glossary
  - Acknowledgments
  - Sitemap

Search
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using a local clone.

# core.sync.mutex

The mutex module provides a primitive for maintaining mutually exclusive access.
License:
Boost License 1.0
Authors:
Sean Kelly

Source: core/sync/mutex.d

class Mutex: object.Object.Monitor;
  This class represents a general purpose, recursive mutex.
  Examples:

      auto mutex      = new Mutex;
      int  numThreads = 10;
      int  numTries   = 1000;
      int  lockCount  = 0;

      void testFn()
      {
          for( int i = 0; i < numTries; ++i )
          {
              synchronized( mutex )
              {
                  ++lockCount;
              }
          }
      }

      auto group = new ThreadGroup;

      for( int i = 0; i < numThreads; ++i )
          group.create( &testFn );

      group.joinAll();
      assert( lockCount == numThreads * numTries );

  nothrow @trusted this();
    Initializes a mutex object.
    Throws:
    SyncError on error.
  nothrow @trusted this(Object o);
    Initializes a mutex object and sets it as the monitor for o.

    In: o must not already have a monitor.

  @trusted void lock();
    If this lock is not already held by the caller, the lock is acquired, then the internal counter is incremented by one.
    Throws:
    SyncError on error.
  @trusted void unlock();
    Decrements the internal lock count by one. If this brings the count to zero, the lock is released.
    Throws:
    SyncError on error.
  bool tryLock();
    If the lock is held by another caller, the method returns. Otherwise, the lock is acquired if it is not already held, and then the internal counter is incremented by one.
    Throws:
    SyncError on error.
    Returns:
    true if the lock was acquired and false if not.

Copyright Sean Kelly 2005 - 2009. | Page generated by Ddoc on Mon Jun 27 20:06:52 2016