boost_has_pthreads.ipp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // (C) Copyright John Maddock 2001.
  2. // Use, modification and distribution are subject to the
  3. // Boost Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. // See http://www.boost.org/libs/config for most recent version.
  6. // MACRO: BOOST_HAS_PTHREADS
  7. // TITLE: POSIX Threads
  8. // DESCRIPTION: The platform supports POSIX style threads.
  9. #include <pthread.h>
  10. namespace boost_has_pthreads{
  11. extern "C" void* thread_proc(void* arg)
  12. {
  13. return arg;
  14. }
  15. int test()
  16. {
  17. pthread_mutex_t mut;
  18. int result = pthread_mutex_init(&mut, 0);
  19. if(0 == result)
  20. {
  21. //
  22. // Failure to be able to create and use a mutex
  23. // is always a failure, even if the pthread
  24. // library is just a non-functioning stub.
  25. //
  26. result |= pthread_mutex_lock(&mut);
  27. result |= pthread_mutex_unlock(&mut);
  28. result |= pthread_mutex_trylock(&mut);
  29. result |= pthread_mutex_unlock(&mut);
  30. result |= pthread_mutex_destroy(&mut);
  31. //
  32. // Try and create a thread, this is allowed
  33. // to fail, in case we are linking to a pthread
  34. // "stub" library.
  35. //
  36. pthread_t t;
  37. int r = pthread_create(&t, 0, &thread_proc, 0);
  38. // result |= r;
  39. if(r == 0)
  40. {
  41. //
  42. // If we can create a thread, then we must be able to join to it:
  43. //
  44. void* arg;
  45. r = pthread_join(t, &arg);
  46. result |= r;
  47. }
  48. }
  49. return result;
  50. }
  51. }