Problems of boost and multithread

below are my codes

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
class equalOut
{
  public :
    typedef bool result_type;
    template<typename T, typename U>
    result_type const operator()(T const A, U const B) const
    {
      std::cout<<A.size()<<", "<<B<<std::endl;
      return A.size() == B;
    }
};

boost::mutex mut;
std::vector<int> int_vector;
boost::condition_variable int_cond;

void data_preparation_thread()
{
  while(int_vector.size() != 10)
  {
    std::cout<<"front :: data preparation thread :: vector size = "<<int_vector.size()<<std::endl;
    boost::lock_guard<boost::mutex> lk(mut);
    int_vector.push_back(4);
    int_cond.notify_one();
    std::cout<<"back :: data preparation thread :: vector size = "<<int_vector.size()<<std::endl;
  }
}

void data_processing_thread()
{
  while(1)
  {
    std::cout<<"front :: data processing thread :: vector size = "<<int_vector.size()<<std::endl;
    boost::unique_lock<boost::mutex> lk(mut);
    std::cout<<"back :: data processing thread :: vector size = "<<int_vector.size()<<std::endl;
    //int_cond.wait(lk, boost::bind( std::equal_to<int>(),  int_vector.size(), 10) );
    //int_cond.wait(lk, boost::bind( equalOut<int>(),  int_vector.size(), 10) );
    //int_cond.wait(lk, boost::bind( equalOut<std::vector<int>, size_t>(),  boost::cref(int_vector), 10) );
    int_cond.wait(lk, boost::bind( equalOut(),  boost::cref(int_vector), static_cast<size_t>(10) ) );
    std::cout<<"int_cond = true\n";
    lk.unlock();
    break;
  }
}

void testCondVar()
{
  boost::thread t1(data_processing_thread);
  boost::thread t2(data_preparation_thread);
  t1.join();
  t2.join();
}

int main()
{
  testCondVar();
  return 0;
}


The results

front :: data processing thread :: vector size = 0
back :: data processing thread :: vector size = 0
0, 10
front :: data preparation thread :: vector size = 0
front :: data preparation thread :: vector size = 1
front :: data preparation thread :: vector size = 2
front :: data preparation thread :: vector size = 3
front :: data preparation thread :: vector size = 4
front :: data preparation thread :: vector size = 5
front :: data preparation thread :: vector size = 6
front :: data preparation thread :: vector size = 7
front :: data preparation thread :: vector size = 8
front :: data preparation thread :: vector size = 9
10, 10
int_cond = true


my problems is, how could the int_cond.notify_one() only notify the thread t1 two times?
Thank you very much
Last edited on
my problems is, how could the int_cond.notify_one() only notify the thread t1 two times?
No, it's notified just once. The first check is done when it enters 'wait()'.

The 2. check is done after a the first notification received. The reason why it gets only one notification is that 'data_preparation_thread()' doesn't give up its time slice until it's finished.
No, it's notified just once. The first check is done when it enters 'wait()'.

Thanks, it was my mistake

The reason why it gets only one notification is that 'data_preparation_thread()' doesn't give up its time slice until it's finished.

According to your explanation, the boost::condition_variable do not guatantee it would
notify the thread when it reach notify_one or notify_all?
The notify function is non-blocking, so the data_preparation_thread would execute as fast as
it could?How many times the notify_one would notify data_processing_thread would depend
on the schedule of the OS?

The other question is, I am using a single core cpu to practice concurrency programming
what would boost::thread handle the case like this?
for now, all of my programs act like a single thread program

Thanks a lot
According to your explanation, the boost::condition_variable do not guatantee it would
notify the thread when it reach notify_one or notify_all?
Yes, it sets the appropriate condition but it doesn't care whether or when another thread checks the condition.

The notify function is non-blocking, so the data_preparation_thread would execute as fast as
it could?
Yes

How many times the notify_one would notify data_processing_thread would depend
on the schedule of the OS?
if that conditional variable is not counting yes.

The other question is, I am using a single core cpu to practice concurrency programming
what would boost::thread handle the case like this?
I'm not sure what you mean by that. But since threads are attached to a process they will run on that cpu with the mentioned time slice
Topic archived. No new replies allowed.