Hi,

I'm using Borland C++Builder V5.0.

I simply want to pass a variable between two C++ TThreads. I searched a lot on the Internet but can't find anything like that. Simply copying by calling another threads function or property is no option. The compiler thinks it's okay but when executing the program disagrees. ;-)

Al tips and hints are welcome.

Thanks in advance,

Henk

use global variables. You will have to synchronize access to the variables so that one thread doesn't try to read it while another thread is writing to it.

If you are writing a MS-Windows GUI program then you might be able to call SendThreadMessage().

Hi,

thanks for your replay, but,

this partly works but not completely. Maybe I have to explain it a little further.

- My object F12 dynamically creates a TThread F15.
- My thread F15 dynamically creates a TThread F16.

Now I want to pass some data from F12 into F16 (right through or bypassing F15)

F12 can successfully modify some F15 global variable.

But my F15 thread cannot access a F16 function to pass this variable further on to F16.

Hope this makes sense.

Henk

Hi,

Well I think I found a solution myself...

- the constructor of TThread F15 creates TThread F16
- this constructor then fills a global variable with a pointer to F16
- when F12 calls a function in F15 then that F15 function calls a function in F16 by using the global F16 TThread object pointer filled by the F15 constructor

I dunno if this is real save programming but at least it works!

Thanks for listening anyway.

Henk

If you are writing a MS-Windows GUI program then you might be able to call SendThreadMessage().

There's no such thing as SendThreadMessage(). You were probably referring to PostThreadMessage(), but that would be an option only if the thread that you need to communicate to has a message pump, and is actively pumping messages.

Hi,

Usually, the thread creation function takes in a void * as one of the parameters. You pass in the address of the variable to be shared in this place. The thread then casts it to the appropriate type and uses it. If the object was created on the heap, then the thread could consider deleting it after usage.

Take a look at this page for an example: http://www.codeproject.com/KB/threads/crtmultithreading.aspx

It isn't the best of the examples, but it should get you started. Also, this page has some excellent information: http://www.flounder.com/workerthreads.htm

Or if you were wanting to communicate between two threads that are running, there is something known as event (which you can "set" from one thread, so that the other thread senses it).

It's a fairly complex subject for a forum post like this, and I suggest that you read some articles and probably a book on multi-threading.

This question has already been answered. Start a new discussion instead.