I have to learn how to deal with socket programming because of my job(I don't have any experience about it)
My company ask me to learn ACE because I have to maintain our ex and new products

After I google the information from the internet, most of the users say asio is a much more better choice than ACE
if you only want to deal with socket programming. But they didn't say about the performance within this two libraries

Many users claim that ACE is a heavy framework(and poor design)?

I have three questions

One : is asio as robust as ACE?
although many users complain that ACE is hard to use and a poor
design heavy framework, no one do not agree that ACE is solid

Two : the performance between asio and ACE

Three : could I use asio and ACE together?

Recommended Answers

All 10 Replies

> is asio as robust as ACE?

Probably not. ACE has been in wide use for a much longer time.

> the performance between asio and ACE

ACE is heavyweight in terms of memory footprint; asio is not.

In terms of speed, it shouldn't make a difference: you can execute a few million instructions in the time it takes to send a single packet over TCP/IP.

> could I use asio and ACE together?

You certainly could. But ACE does tend to impose a particular architectural structure for your program, which it supports quite well; with asio you might have to write some extra code to conform to that architecture.

looks like I don't have to worry about the problems of performance

The other problem is asio steady or not?
Atleast I haven't heard any bad news about that

Just food for thought:
Performance of network programming mainly depends on how you are processing your datas.
How you are sharing the data among several component of your software?
How you are sharing the data among several processes on the machine? (shared memory/pipe/file fifo, mapped memory)
How you have distributed load and data among the cores? use of data structure. use of L1,L2,L3 caches.
Threads and locking: what is spin lock, what is mutex, read/write lock, semaphore, condition variables? thread specific data?
Locality matters! what does that mean?
small object allocation.
dont do premature optimization.
what is lock free algorithm?
what is wait free algorithm?
How can we use atomic operations?


I thought I will explain those questions, but each of those will be big article, so just a guideline for further reading.

I am learning how to deal with multi thread from the book "c++ concurrency in action"
study how to design an allocator to handle small objects(any good suggestion
about free, robust allocator which design for small objects?)

Looks like socket programming is much more difficult than I could imagine

Suggestions made by vijayan121 is very good and I would have made the same suggestions.
But dont worry about small object allocation if you are only started to learn thread and concurrency.

First I would suggest to read about threads and different kind of locks and also lock free algorithms.

Here is an example of lock free algorithm which will use L1 and L2 cache, will try to avoid false sharing.

Lets say you have quad core pentium i5/i7 machine and you want to sort 100 million integers. your L1 cache size is 256 byte. and L2 cache size is 1 MB (cache size differs from machine to machine, you can use getconf on linux to find the cache size.)

So a quad core on i5/i7 will have two hardware thread per core which will share L1 instruction cache and data cache.

Anyway, you want to create 4*2 (hardware thread ) = 8 thread at least to sort your 100 million integers.

You will use very simple data structure, vector (as it access row mejor order and contiguous memory ) and will use very simple sorting algorithm, merge sort.

Merge sort uses the basic divide and conquer mechanism which we will use to divide the load as well.

So you want to divide your full data set into 8 parts as you will have 8 threads.
When you hit L2 cache size, 1 MB of data, you might not want to divide any more. You certainly wont divide less than L1 cache size. other wise it will hit the cache coherency issue and cache misses will increase exponentially.

which will also make sure you are not doing any false sharing.

Run it and see performance, you will be amazed what C++ programmer can do what other language programmer cant?

:)

First I would suggest to read about threads and different kind of locks and also lock free algorithms.

Thanks for your advices.

Recently my company ask me to study the xerces c++
After study xerces c++, I realize why so many people would say Qt4 is a well
designed library and is a good lesson for oop.
The api of Qt4 are clear and ease to use; you don't need to use dynamic_cast nor
static_cast frequently; the memory is very easy to manage and so on

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( equalOut(),  boost::cref(int_vector), static_cast<size_t>(10) ) );
    std::cout<<"int_cond = true\n";
    lk.unlock();
    break;
  }
}

These codes come from the book "c++ concurrency in anction"(I change it a little bit)
According to the book(if I do not take a wrong pace)
The condition variable would only awake the data_processing_thread if and only if the condition_variable is waiting for the signal already
that means the data_prepation_thread may notify the data_processing_thread even
it do not get into the waiting condition

What if data_preparation_thread already meet the end requirement but the
data_processing_thread did not get into the waiting condition yet?
Do we have to handle the predicate by ourself?

while(int_vector.size() != 10)

(is this call predicate?)

Thanks a lot

Condition variable is a mechanism to implement producer/consumer idiom.

Producer will put data/job into a queue.
Consumer will deque the queue and work on that data.

Usually you should have one producer thread and multiple consumer threads.
In your case one data preparation thread and multiple data processing threads.

I haven't worked much on boost but looks like you haven't implement the data_prepartaion_thread right.

Because this logic while(int_vector.size() != 10) shouldn't full fill unless all of your consumer threads are busy and your queue is over flown (which still can happened on the busy server, but i have a feeling that wasn't your intention, i think you wanted to enter 10 jobs)

The condition variable would only awake the data_processing_thread if and only if the condition_variable is waiting for the signal already

That means if none of the data processing threads were on the conditional wait, then signal might get loss.

But its very easy to fix. because your data_processing_thread shouldn't sleep/wait on condition variable if there is already data/job available to process.

So before it goes to sleep, it should check if there is any data available in the queue. If there is data, then no point of sleeping, lets process those. if there is no data, then lets sleep, and Producer_thread/data_preparation thread will signal/broadcast when data is available.

Two advantage of this: 1) It fixes your problem
2)condition variable is expensive (if you are thinking in billions of jobs), it uses mutex( and mutex uses futex system call which is expensive)
you can improve performance by 25% at least.


You can do other mechanism to make condition variable perform. Have a look into the following article:
Make Condition variable perform

Thanks a lot, multi thread is difficult but interesting

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.