Multithreading

4 views
Skip to first unread message

omn...@mario.n-ten.de

unread,
Jul 2, 2011, 9:10:31 AM7/2/11
to SpoVNet
Hello,

I have a "send" funtion running in an own thread. It consits of a loop
sending a bunch of messages. But sometimes/often the last message
never gets to the other end.

I guess the send-function finishes before all messages are sent and
the message-object created in the send-thread gets deleted while being
in the "outgoing"-queue. But I'm not sure what to do against that. I
have to delete the message in the send function, because ariba won't
do that for me and I have to wait until the message is sent, which I
don't know when it is.

Probably I'll send an ACK when all data is received and block the
sending thread 'til this ACK is received. But that seems more like a
workaround than a proper solution.

Any suggestion how to solve this issue?

This also raises the question if and when messages are copied. May I
reuse a buffer or a message-object after passing it to sendMessage?

Best,
Mario Hock

Christoph Mayer

unread,
Jul 5, 2011, 4:29:27 AM7/5/11
to SpoVNet
Hi, at best you create message objects for every send operation:

for node in mynodes:
MyMessage m;
m.setType();
m.setMyInfo();
node->sendMessage(m);

After the sendMessage call the message object may become invalid.

Does this help?
Chris

On 2 Jul., 15:10, "omni...@mario.n-ten.de" <omni...@mario.n-ten.de>
wrote:

omn...@mario.n-ten.de

unread,
Jul 7, 2011, 9:29:22 AM7/7/11
to SpoVNet
Hi,

what about:

Buffer b(4096); // some kind of buffer-object
while ( b.readData() ) // read data from a file into the buffer b
MyMessage m(b);
m.setType();
m.setMyInfo();
node->sendMessage(m);
//sleep(1); // outside the loop but in the same function

As the Message Object can be freed after the send operation, I suppose
I can reuse the buffer b?

Problem is, my send-function looks like the code above, but often the
latest Message won't be send. The program does not crash, just one
Message is missing. The "sleep(1)" would solve the problem for now,
but of course is no reasonable solution.

So, if the Message Object m is no longer needed after calling
sendMessage, what happens to the last message?

Thanks, Mario

Christoph Mayer

unread,
Jul 9, 2011, 5:55:59 AM7/9/11
to SpoVNet, mi...@kit.edu
A message is serialized, put into a queue, and _then_ the sendMessage
function returns. Therefore I don't understand how this can happen.
Are the messages that to get send correctly delivered to the
destination?

@Basti in CC: any ideas?

Best,
Chris

On 7 Jul., 15:29, "omni...@mario.n-ten.de" <omni...@mario.n-ten.de>
wrote:

Sebastian Mies

unread,
Jul 12, 2011, 8:30:57 AM7/12/11
to spo...@googlegroups.com
Hi Mario,

I suppose you read the data in a blocking manner, right?
Or in which thread do you read data / send messages?
That the sleep does the trick is a indicator, that
you may suffer a concurrency problem. This is kinda hard
to debug remotely.

What you should do is: use a thread to read data
and dispatch the message sending to the ariba main thread
(typical asynchronous design pattern). Then you might get
the problem, that you indeed can't reuse the buffer, because
the message only copies the buffer on serialization depending
on your message's implementation.

You may also check if the memory is valid via valgrind
or similar. Usually the message is copied on each layer
(not optimal, but works *g*) therefore you may re-use
the buffer.

Hope this helps.

Best regards,

Sebastian Mies

--
Sebastian Mies
Gebhardstraße 25
76137 Karlsruhe

Telefon: +49 721 9119627
E-Mail: mi...@gtfr.de

Christoph Mayer

unread,
Jul 14, 2011, 9:32:40 AM7/14/11
to SpoVNet
Some follow-up: normally you should always run in the main ariba-
thread and develop in an event-based style. This means that you should
nevery run longer operations or block, as this will lock the complete
thread. If you have to perform longer operations, have a look at the
BlockingMethod class under utility/system. This allows you to request
a new thread and provides ways to easily re-integrate into the ariba
main thread.

Best,
Chris

On 12 Jul., 14:30, Sebastian Mies <m...@gtfr.de> wrote:
> Hi Mario,
>
> I suppose you read the data in a blocking manner, right?
> Or in which thread do you read data / send messages?
> That the sleep does the trick is a indicator, that
> you may suffer a concurrency problem. This is kinda hard
> to debug remotely.
>
> What you should do is: use a thread to read data
> and dispatch the message sending to the ariba main thread
> (typical asynchronous design pattern). Then you might get
> the problem, that you indeed can't reuse the buffer, because
> the message only copies the buffer on serialization depending
> on your message's implementation.
>
> You may also check if the memory is valid via valgrind
> or similar. Usually the message is copied on each layer
> (not optimal, but works *g*) therefore you may re-use
> the buffer.
>
> Hope this helps.
>
> Best regards,
>
> Sebastian Mies
>
> E-Mail: m...@gtfr.de
Reply all
Reply to author
Forward
0 new messages