test_destroy_locked_mutex.cpp 604 B

12345678910111213141516171819202122232425262728
  1. //test_destroy_locked_mutex.cpp
  2. #include <pthread.h>
  3. #include <stdio.h>
  4. #include <errno.h>
  5. int main()
  6. {
  7. pthread_mutex_t mymutex;
  8. pthread_mutex_init(&mymutex, NULL);
  9. int ret = pthread_mutex_lock(&mymutex);
  10. //尝试对被锁定的mutex对象进行销毁
  11. ret = pthread_mutex_destroy(&mymutex);
  12. if (ret != 0)
  13. {
  14. if (ret == EBUSY)
  15. printf("EBUSY\n");
  16. printf("Failed to destroy mutex.\n");
  17. }
  18. ret = pthread_mutex_unlock(&mymutex);
  19. //尝试对已经解锁的mutex对象进行销毁
  20. ret = pthread_mutex_destroy(&mymutex);
  21. if (ret == 0)
  22. printf("Succeeded to destroy mutex.\n");
  23. return 0;
  24. }