Original Chexlong original address: http://blog.csdn.net/chexlong/article/details/7058283
In the previous article in C + + implementation of the WIN32 platform on the multi-threaded mutex, this time to write a Linux platform, the same reference to the Open source project C + + sockets code, in this to the open source projects to contribute to the Warriors to express gratitude!
Below are mutually exclusive lock classes and test codes, which have been tested on Fedora 13 virtual machines.
Lock.h
[CPP]View Plaincopy
- #ifndef _lock_h
- #define _lock_h
- #include <pthread.h>
- Lock Interface Class
- Class ILock
- {
- Public
- Virtual ~ilock () {}
- virtual void Lock () const = 0;
- virtual void Unlock () const = 0;
- };
- Mutex Lock class
- Class CMutex: Public ILock
- {
- Public
- CMutex ();
- ~cmutex ();
- virtual void Lock () const;
- virtual void Unlock () const;
- Private
- mutable pthread_mutex_t M_mutex;
- };
- Lock
- Class Cmylock
- {
- Public
- Cmylock (const ilock&);
- ~cmylock ();
- Private
- Const ilock& M_lock;
- };
- #endif
Lock.cpp
[CPP]View Plaincopy
- #include "Lock.h"
- Dynamic initialization of Mutex locks
- Cmutex::cmutex ()
- {
- Pthread_mutex_init (&m_mutex, NULL);
- }
- Unregister a mutex lock
- Cmutex::~cmutex ()
- {
- Pthread_mutex_destroy (&m_mutex);
- }
- Ensure that the thread that owns the mutex accesses the protected resource on its own
- void Cmutex::lock () const
- {
- Pthread_mutex_lock (&m_mutex);
- }
- Frees the lock owned by the current thread so that other threads can have a mutex to access the protected resource
- void Cmutex::unlock () const
- {
- Pthread_mutex_unlock (&m_mutex);
- }
- Automatic lock-out with C + + features
- Cmylock::cmylock (const ilock& m): M_lock (m)
- {
- M_lock. Lock ();
- }
- Automatic unlocking with C + + features
- Cmylock::~cmylock ()
- {
- M_lock. Unlock ();
- }
Test code
[CPP]View Plaincopy
- Pthread_mutex.cpp: Defines the entry point of the console application.
- //
- #include <iostream>
- #include <unistd.h>
- #include "Lock.h"
- Using namespace std;
- Create a mutex lock
- CMutex G_lock;
- Thread functions
- void * Startthread (void *pparam)
- {
- char *pmsg = (char *) pparam;
- if (!pmsg)
- {
- return (void *) 1;
- }
- //Lock the protected resource (the following print statement) automatically
- //Before the thread function ends, automatically unlocks
- Cmylock Lock (G_lock);
- For ( int i = 0; i < 5; i++)
- {
- cout << pMsg << Endl;
- Sleep (1);
- }
- return (void *) 0;
- }
- int main (int argc, char* argv[])
- {
- pthread_t thread1,thread2;
- pthread_attr_t attr1,attr2;
- char *PMSG1 = "First print thread.";
- char *pmsg2 = "Second print Thread.";
- //Create two worker threads, print different messages individually
- Pthread_attr_init (&ATTR1);
- Pthread_attr_setdetachstate (&attr1,pthread_create_joinable);
- if (pthread_create (&THREAD1,&ATTR1, STARTTHREAD,PMSG1) = =-1)
- {
- cout<<"Thread 1:create failed" <<endl;
- }
- Pthread_attr_init (&ATTR2);
- Pthread_attr_setdetachstate (&attr2,pthread_create_joinable);
- if (pthread_create (&THREAD2,&ATTR2, startthread,pmsg2) = =-1)
- {
- cout<<"Thread 2:create failed" <<endl;
- }
- //wait for thread to end
- void *result;
- Pthread_join (Thread1,&result);
- Pthread_join (Thread2,&result);
- //Close thread, release resources
- Pthread_attr_destroy (&ATTR1);
- Pthread_attr_destroy (&ATTR2);
- int iwait;
- cin>>iwait;
- return 0;
- }
after the compilation is successful, run the program
Similarly, if you comment out the code below, recompile
[CPP]View Plaincopy
- Cmylock Lock (G_lock);
Run the program
The results are obvious.
Implementing multi-threaded mutexes with C + + on the "Go" Linux Platform