?
Solved

Best way to pass data from a custom thread to the main VCL thread WITHOUT Synchronize

Posted on 2006-11-23
10
Medium Priority
?
493 Views
Last Modified: 2010-04-05
and continuation of this question (http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_22070798.html)

title says it all: we have
- indy
- reading thread on client side
- preferably no synchronize (though I am willing to accept that if it cannot be done otherwise)
- elegant AND simple solutions please (though you can post some interesting "hacks" if you know any (like the one with the timer I mentioned in the other question - keep in mind that it doesn't use a thread so it's no good for me, but it might be good for future references))

PS: in case I missed some obvious solution, my brain is seriously tired :)
0
Comment
Question by:2266180
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +3
10 Comments
 
LVL 19

Accepted Solution

by:
MerijnB earned 400 total points
ID: 18003684
don't have much time to type a long story atm. But what I use often is a system which allocate enough room in a pchar, send the address of the pchar to the receiver (could very well be the main thread). Use PostMessage to make this async. The receiver has to deallocate the memory. If you want to send more then a string you can use a pointer to a record or whatever you can think of :)
0
 
LVL 28

Author Comment

by:2266180
ID: 18003749
windows messages. how could I have missed that? (And I thought about pipes and sinchronized classes and other more complicated things. my brain is tired: misses all the easy stuff.)
so it's a good idea. it's even faster to implement starting with the code I have. thanks.
0
 
LVL 19

Expert Comment

by:MerijnB
ID: 18003797
i'll don't bother for an example, yw :)
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 28

Author Comment

by:2266180
ID: 18003817
no need to :)
thought I will wait for other ideas as well :)
0
 
LVL 21

Assisted Solution

by:ziolko
ziolko earned 400 total points
ID: 18003914
yup, PostMessage() as I posted in previous Q:)

other idea is to create TObjectList descendant with Push() and Pop()
methods so reader will Push() read data and then PostMessage() to reciver
that something is in buffer, reciver will Pop() in this case TCriticalSection will be
needed for push & pop  

ziolko.
0
 
LVL 28

Author Comment

by:2266180
ID: 18003939
I thought about that. issue is that we are doing a communication here (see the demo app in the other Q so I have to consume the data the thread read (by using pop) and the data that waits for me on the connection. using a stack and postmessage will mess up things :)
BUT if you read all need data from the thread, then indeed using a stack and postmessage is an elegant solution.
for this specific case though, just a sendmessage is better and simpler. (reading rest of the data in the message handler)
0
 
LVL 13

Assisted Solution

by:BlackTigerX
BlackTigerX earned 400 total points
ID: 18004077
wouldn't be easier using a memory mapped and simple post messages?
0
 
LVL 17

Expert Comment

by:TheRealLoki
ID: 18004638
if it hasn't been mentioned yet, you could use a TCriticalSection (although I'm not a fan of those in threads personally)
and wm_copydata is another option.
there is also the slow memory method using a mutex for locking, or in fact
the very slow "writing data to a file", and using standard file locking, to know if the main vcl can read from it.
0
 
LVL 17

Assisted Solution

by:TheRealLoki
TheRealLoki earned 400 total points
ID: 18004735
"shred memory" uising a mutex I meant
0
 
LVL 17

Assisted Solution

by:Wim ten Brink
Wim ten Brink earned 400 total points
ID: 18006312
Wished I had something to add but so far the answer seems to have been given already. :-)
I had to deal with a similar situation in the past and solved it in about the same way. One thread would allocate memory on the stack and then Send/Post a message to another thread, allowing the other thread to start processing the data. Worked like a charm, most of the time. Biggest problem is deciding about using either PostMessage or SendMessage. SendMessage will pause the current thread until the message returns with a reply. PostMessage is therefore a bit nicer but its drawback is that when the thread ends that did the PostMessage and the message has not been handled yet, the message will disappear in an eternal limbo.

So for a recent project of mine, I chose a slightly different solution by using a (file-based) queue. Instead of posting messages, you would have one thread pushing items to the queue and the other thread popping the data from the queue. If your queue is just a linked list of pointers then you would only need to use a critical section in the push/pop functions to protect the queue. (But not the data!) Your queue item would be something like this:

type
  PQueueItem = ^TQueueItem;
  TQueueItem=record
    Data: Pointer; // Points to your data structure.
    Next: PQueueItem;
  end;

Creating this item (with New) can be done outside the critical section. It's just the adding and deleting of queue items that needs to be protected. And while I use a file-based queue since my data happens to be quite big, you could just keep this queue in-memory, of course.

Still, a solution like this queue has also been suggested before in this thread. :-)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses
Course of the Month12 days, 17 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question