Ice Reading Notes-C ++ threads and concurrency (1)

Source: Internet
Author: User

C ++ threads and concurrency

1. Ice thread model

The ice server is multi-threaded. The server run time maintains a thread pool for processing incoming requests. Through the leader-follower thread model, each operation call sent by the customer is distributed in its own thread.

Multithreading means that multiple calls from customers can be concurrently executed on the server. The ice thread Library provides many synchronization primitives, such as simple mutex, read/write locks, and monitors. These synchronization primitives allow concurrency control at different levels.

2. Thread library Overview

The ice thread Library provides several thread-related abstractions: mutex, recursive mutex, read/write recursive mutex, monitor, and a thread abstraction, allows developers to create, control, and destroy threads (the thread API is part of the iceutil namespace ).

3. mutex

Iceutil: mutex class (defined in iceutil/mutex. h) and iceutil: staticmutex (defined in iceutil/staticmutex. h) provide a simple non-recursive mutex mechanism:

namespace IceUtil {   class Mutex {   public:      Mutex();      ~Mutex();      void lock() const;      bool tryLock() const;      void unlock() const;      typedef LockT<Mutex> Lock;      typedef TryLockT<Mutex> TryLock;   };   struct StaticMutex {      void lock() const;      bool tryLock() const;      void unlock() const;      typedef LockT<StaticMutex> Lock;      typedef TryLockT<StaticMutex> TryLock;   };}

Suppose there is a file class named filesystem, which has a read and write operation function (for example, write ):

#include <IceUtil/Mutex.h>// ...namespace Filesystem {// ...   class FileI : virtual public File,virtual public Filesystem::NodeI {   public:   // As before...   private:      Lines _lines;      IceUtil::Mutex _fileMutex;   };// ...}
Filesystem::Lines;

void Filesystem::FileI::write(const Filesystem::Lines & text,const Ice::Current &){    _lines = text; // Not thread safe!}

After the thread mechanism is added:

 
void Filesystem::FileI::write(const Filesystem::Lines & text,const Ice::Current &){    _fileMutex.lock();    _lines = text;    _fileMutex.unlock();}

However, the use of such lock and unlock operations has a fixed problem: If the write unlock operation is missing in the program, or the program has returned before the unlock operation is completed, A deadlock occurs.

To unlock the mutex lock, the mutex class contains two types of helper classes: Lock and trylock:

namespace IceUtil {   class Mutex {   // ...      typedef LockT<Mutex> Lock;      typedef TryLockT<Mutex> TryLock;   };}

Lockt and trylockt are simple templates, mainly composed of constructor and destructor;The constructor calls lock for its parameters, while the Destructor calls unlock.Rewrite the write function now:

void Filesystem::FileI::write(const Filesystem::Lines & text,const Ice::Current &){   IceUtil::Mutex::Lock lock(_fileMutex);    _lines = text;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.