Implementing multi-threaded mutexes with C + + on the "Go" Linux Platform

Source: Internet
Author: User

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
  1. #ifndef _lock_h
  2. #define _lock_h
  3. #include <pthread.h>
  4. Lock Interface Class
  5. Class ILock
  6. {
  7. Public
  8. Virtual ~ilock () {}
  9. virtual void Lock () const = 0;
  10. virtual void Unlock () const = 0;
  11. };
  12. Mutex Lock class
  13. Class CMutex: Public ILock
  14. {
  15. Public
  16. CMutex ();
  17. ~cmutex ();
  18. virtual void Lock () const;
  19. virtual void Unlock () const;
  20. Private
  21. mutable pthread_mutex_t M_mutex;
  22. };
  23. Lock
  24. Class Cmylock
  25. {
  26. Public
  27. Cmylock (const ilock&);
  28. ~cmylock ();
  29. Private
  30. Const ilock& M_lock;
  31. };
  32. #endif

Lock.cpp

[CPP]View Plaincopy
  1. #include "Lock.h"
  2. Dynamic initialization of Mutex locks
  3. Cmutex::cmutex ()
  4. {
  5. Pthread_mutex_init (&m_mutex, NULL);
  6. }
  7. Unregister a mutex lock
  8. Cmutex::~cmutex ()
  9. {
  10. Pthread_mutex_destroy (&m_mutex);
  11. }
  12. Ensure that the thread that owns the mutex accesses the protected resource on its own
  13. void Cmutex::lock () const
  14. {
  15. Pthread_mutex_lock (&m_mutex);
  16. }
  17. Frees the lock owned by the current thread so that other threads can have a mutex to access the protected resource
  18. void Cmutex::unlock () const
  19. {
  20. Pthread_mutex_unlock (&m_mutex);
  21. }
  22. Automatic lock-out with C + + features
  23. Cmylock::cmylock (const ilock& m): M_lock (m)
  24. {
  25. M_lock. Lock ();
  26. }
  27. Automatic unlocking with C + + features
  28. Cmylock::~cmylock ()
  29. {
  30. M_lock. Unlock ();
  31. }

Test code

[CPP]View Plaincopy
  1. Pthread_mutex.cpp: Defines the entry point of the console application.
  2. //
  3. #include <iostream>
  4. #include <unistd.h>
  5. #include "Lock.h"
  6. Using namespace std;
  7. Create a mutex lock
  8. CMutex G_lock;
  9. Thread functions
  10. void * Startthread (void *pparam)
  11. {
  12. char *pmsg = (char *) pparam;
  13. if (!pmsg)
  14. {
  15. return (void *) 1;
  16. }
  17. //Lock the protected resource (the following print statement) automatically
  18. //Before the thread function ends, automatically unlocks
  19. Cmylock Lock (G_lock);
  20. For ( int i = 0; i < 5; i++)
  21. {
  22. cout << pMsg << Endl;
  23. Sleep (1);
  24. }
  25. return (void *) 0;
  26. }
  27. int main (int argc, char* argv[])
  28. {
  29. pthread_t thread1,thread2;
  30. pthread_attr_t attr1,attr2;
  31. char *PMSG1 = "First print thread.";
  32. char *pmsg2 = "Second print Thread.";
  33. //Create two worker threads, print different messages individually
  34. Pthread_attr_init (&ATTR1);
  35. Pthread_attr_setdetachstate (&attr1,pthread_create_joinable);
  36. if (pthread_create (&THREAD1,&ATTR1, STARTTHREAD,PMSG1) = =-1)
  37. {
  38. cout<<"Thread 1:create failed" <<endl;
  39. }
  40. Pthread_attr_init (&ATTR2);
  41. Pthread_attr_setdetachstate (&attr2,pthread_create_joinable);
  42. if (pthread_create (&THREAD2,&ATTR2, startthread,pmsg2) = =-1)
  43. {
  44. cout<<"Thread 2:create failed" <<endl;
  45. }
  46. //wait for thread to end
  47. void *result;
  48. Pthread_join (Thread1,&result);
  49. Pthread_join (Thread2,&result);
  50. //Close thread, release resources
  51. Pthread_attr_destroy (&ATTR1);
  52. Pthread_attr_destroy (&ATTR2);
  53. int iwait;
  54. cin>>iwait;
  55. return 0;
  56. }

after the compilation is successful, run the program

Similarly, if you comment out the code below, recompile

[CPP]View Plaincopy
    1. Cmylock Lock (G_lock);

Run the program

The results are obvious.

Implementing multi-threaded mutexes with C + + on the "Go" Linux Platform

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.