summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/thread/test/test_7755.cpp
blob: 5551a6bb9afeb2112a56eea52ce578ba9ff077ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright (C) 2013 Vicente Botet
//
//  Distributed under the Boost Software License, Version 1.0. (See accompanying
//  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

////////////////////////////////////////////

//#define BOOST_THREAD_PROVIDES_GENERIC_SHARED_MUTEX_ON_WIN
#include <iostream>
#include <boost/thread/thread_only.hpp>
#include <boost/thread/shared_mutex.hpp>
// shared_mutex_deadlock.cpp : Defines the entry point for the console application.
//


boost::shared_mutex mutex;

void thread2_func()
{
  int i (0);
  for (;;)
  {
    if (mutex.timed_lock(boost::posix_time::milliseconds(500)))
    {
      std::cout << "Unique lock acquired" << std::endl
        << "Test successful" << std::endl;
      mutex.unlock();
      break;
    }
    ++i;
    if (i == 100)
    {
      std::cout << "Test failed. App is deadlocked" << std::endl;
      break;
    }
    boost::this_thread::sleep(boost::posix_time::seconds(1));
  }
}

void thread3_func()
{
  boost::shared_lock<boost::shared_mutex> lock (mutex);
  std::cout << "Shared lock acquired" << std::endl
        << "Test successful" << std::endl;
}

void thread3_func_workaround()
{
  for (;;)
  {
    if (mutex.timed_lock_shared(boost::posix_time::milliseconds(200)))
    {
      std::cout << "Shared lock acquired" << std::endl
        << "Test successful" << std::endl;
      mutex.unlock_shared();
      break;
    }
    boost::this_thread::sleep(boost::posix_time::milliseconds(100));
  }
}

int main()
{
  std::cout << "Starting" << std::endl;

  // 1 - lock the mutex
  boost::shared_lock<boost::shared_mutex> lock (mutex);

  // 2 - start thread#2
  boost::thread thread2(&thread2_func);

  // 3 - sleep
  boost::this_thread::sleep(boost::posix_time::milliseconds(10));

  std::cout << "Thread#2 is waiting" << std::endl;

  // - start thread3
  boost::thread thread3(&thread3_func);
  //boost::thread thread3(&thread3_func_workaround);

  std::cout << "Thread#3 is started and blocked. It never will waked" << std::endl;

  thread3.join(); // will never return

  lock.unlock(); // release shared ownership. thread#2 will take unique ownership

  thread2.join();

  std::cout << "Finished" << std::endl;
  return 0;
}