Esempio n. 1
0
void multithread_push_pop(int64_t counts)
{
  std::atomic_bool start(false);
  std::atomic_bool quit(false);
  int64_t push_sum = 0;
  int64_t pop_sum1 = 0, pop_sum2 = 0;
  WSQ_t q;
  std:: thread thrd1([&start, counts, &push_sum, &pop_sum1, &q, &quit](){
      while(!start) std::this_thread::yield();
      std::chrono::time_point<std::chrono::system_clock> start, end;
      start = std::chrono::system_clock::now();
      for(int64_t i =0; i < counts; ++i){
        if(q.push_back(i))
          push_sum += i;
        if(i % 3 == 0)
        {
          int64_t t;
          if(q.pop(t)) pop_sum1 += t;
        }
      }
      while(q.size() != 0){
      int64_t t;
      if(q.pop(t)) pop_sum1 +=t;
      }
      quit = true;
      end = std::chrono::system_clock::now();
      auto elapsed_seconds = std::chrono::duration_cast<std::chrono::microseconds>(end-start).count();
      g_print_mutex.lock();
      std::cout<<"push/pop thread : "<<elapsed_seconds<<std::endl;
      g_print_mutex.unlock();
      });

  std::thread thrd2([&start, &pop_sum2, &q, & quit](){
      while(!start) std::this_thread::yield();
      std::chrono::time_point<std::chrono::system_clock> start, end;
      start = std::chrono::system_clock::now();
      while(!quit)
      {
        int64_t t;
        if(q.steal(t)){
          pop_sum2 += t;
        }
      }
      end = std::chrono::system_clock::now();
      auto elapsed_seconds = std::chrono::duration_cast<std::chrono::microseconds>(end-start).count();
      g_print_mutex.lock();
      std::cout<<"steal thread : "<<elapsed_seconds<<std::endl;
      g_print_mutex.unlock();
      });
  start = true;

  thrd1.join();
  thrd2.join();

  if(push_sum != pop_sum1 + pop_sum2)
  {
    std::cout<<"bugs here! push sum is: "<<push_sum<<", pop_sum is "<<pop_sum1 + pop_sum2<<std::endl;
  }

}
Esempio n. 2
0
int main()
{
    boost::thread thrd1(&sender);
    boost::thread thrd2(&receiver);
    thrd1.join();
    thrd2.join();
}
Esempio n. 3
0
int main(int argc, char* argv[]) {
  boost::thread thrd1(count(1));
  boost::thread thrd2(count(2));
  thrd1.join();
  thrd2.join();
  return 0;
}
Esempio n. 4
0
int main(int argc, char* argv[])
{
  std::thread thrd1(std::bind(&count, 1));
  std::thread thrd2(std::bind(&count, 2));
  thrd1.join();
  thrd2.join();
  return 0;
}
Esempio n. 5
0
int main(int argc, char* argv[])
{
  boost::thread thrd1(&reader);
  boost::thread thrd2(&writer);
  thrd1.join();
  thrd2.join();
  return 0;
}
Esempio n. 6
0
int main(int, char*[])
{
    boost::thread thrd1(&sender);
    boost::thread thrd2(&receiver);
    thrd1.join();
    thrd2.join();
    return 0;
}
Esempio n. 7
0
void do_test(M* dummy=0)
{
    typedef buffer_t<M> buffer_type;
    buffer_type::get_buffer();
    boost::thread thrd1(&buffer_type::do_receiver_thread);
    boost::thread thrd2(&buffer_type::do_receiver_thread);
    boost::thread thrd3(&buffer_type::do_sender_thread);
    thrd1.join();
    thrd2.join();
    thrd3.join();
}
Esempio n. 8
0
int main() {
  printf("\n============== sizeof(boost::thread) ============== %d\n", sizeof(boost::thread));
  printf("\n============== sizeof(boost::detail::thread_data_base) ============== %d\n", sizeof(boost::detail::thread_data_base));
  printf("\n============== sizeof(boost::detail::thread_data<>) ============== %d\n", sizeof(boost::detail::thread_data<void(*)()>));
  printf("\n============== Before thread creation ==============\n");
  display_mallinfo();
  {
    boost::thread thrd1(&count);

    printf("\n============== After thread creation ==============\n");
    display_mallinfo();

    boost::thread thrd2(&count);
    printf("\n============== After thread creation ==============\n");
    display_mallinfo();
    boost::thread thrd3(&count);
    printf("\n============== After thread creation ==============\n");
    display_mallinfo();

    thrd1.join();
    printf("\n============== After thread join ==============\n");
    display_mallinfo();

    thrd2.join();
    printf("\n============== After thread join ==============\n");
    display_mallinfo();
    thrd3.join();
    printf("\n============== After thread join ==============\n");
    display_mallinfo();
  }
  printf("\n============== After thread destruction ==============\n");
  display_mallinfo();

  {
    pthread_attr_t attr;
    pthread_attr_init(&attr);

    pthread_t thrd1;
    pthread_create(&thrd1, &attr, &count2, 0);
    printf("\n============== After thread creation ==============\n");
    display_mallinfo();

    pthread_t thrd2;
    pthread_create(&thrd2, &attr, &count2, 0);
    printf("\n============== After thread creation ==============\n");
    display_mallinfo();

    pthread_t thrd3;
    pthread_create(&thrd3, &attr, &count2, 0);
    printf("\n============== After thread creation ==============\n");
    display_mallinfo();

    pthread_attr_destroy(&attr);
    printf("\n============== After thread attr destroy ==============\n");
    display_mallinfo();

    void* res;
    pthread_join(thrd3, &res);
    printf("\n============== After thread join ==============\n");
    display_mallinfo();

    pthread_join(thrd2, &res);
    printf("\n============== After thread join ==============\n");
    display_mallinfo();
    pthread_join(thrd1, &res);
    printf("\n============== After thread join ==============\n");
    display_mallinfo();



    //pthread_destroy(&thrd1);
    //pthread_destroy(&thrd2);
    //pthread_destroy(&thrd3);
  }
  printf("\n============== After thread destruction ==============\n");
  display_mallinfo();

    return 1;

}