www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - multithreaded hell, help!

reply Brons <gibbz1 yahoo.com> writes:
Im trying to access data in a global between threads.
So ive set my global to shared, however the object trying to 
access it needs to also then be shared, but it cant be because 
its part of a library...

Is there any simple way to transfer data from one thread to 
another?

1. My situation is im reading from a serial port in a thread. 
Which writes to my global.
2. In a draw thread i need to get that data and draw my output.

Cheers
Jul 21 2016
parent reply rikki cattermole <rikki cattermole.co.nz> writes:
On 21/07/2016 9:44 PM, Brons wrote:
 Im trying to access data in a global between threads.
 So ive set my global to shared, however the object trying to access it
 needs to also then be shared, but it cant be because its part of a
 library...

 Is there any simple way to transfer data from one thread to another?

 1. My situation is im reading from a serial port in a thread. Which
 writes to my global.
 2. In a draw thread i need to get that data and draw my output.

 Cheers
__gshared or cast away shared.
Jul 21 2016
next sibling parent Brons <gibbz1 yahoo.com> writes:
On Thursday, 21 July 2016 at 09:47:09 UTC, rikki cattermole wrote:
 On 21/07/2016 9:44 PM, Brons wrote:
 Im trying to access data in a global between threads.
 So ive set my global to shared, however the object trying to 
 access it
 needs to also then be shared, but it cant be because its part 
 of a
 library...

 Is there any simple way to transfer data from one thread to 
 another?

 1. My situation is im reading from a serial port in a thread. 
 Which
 writes to my global.
 2. In a draw thread i need to get that data and draw my output.

 Cheers
__gshared or cast away shared.
__gshared has done the trick! Thanks :)
Jul 21 2016
prev sibling parent Jonathan M Davis via Digitalmars-d-learn writes:
On Thursday, July 21, 2016 21:47:09 rikki cattermole via Digitalmars-d-learn 
wrote:
 On 21/07/2016 9:44 PM, Brons wrote:
 Im trying to access data in a global between threads.
 So ive set my global to shared, however the object trying to access it
 needs to also then be shared, but it cant be because its part of a
 library...

 Is there any simple way to transfer data from one thread to another?

 1. My situation is im reading from a serial port in a thread. Which
 writes to my global.
 2. In a draw thread i need to get that data and draw my output.

 Cheers
__gshared or cast away shared.
Be _very_ careful of __gshared. It's intended for C-style global variables only - really, with the idea of using them when linking against C code. The type system still treats them as thread-local, not shared, so it can do optimizations based on thread-local which are incompatible with the object being on multiple threads. You may not have much choice in this case, but I'd strongly argue that you avoid using __gshared if possible. An alternative would be marking the object as shared and then casting away shared while the variable is protected by a mutex (or synchronized block) so that you can then call non-shared, member functions on the shared object. If you can do that instead of __gshared, I'd strongly recommend it. However, the preferred way to share information across threads if possible is to use std.concurrency to send messages across threads. It's somewhat limited if you're trying to pass user-defined types across threads if they're not value types (since they have to be immutable or shared), but if you need to, you can cast an object to immutable on one side and back to mutable on the other - so long as you _know_ that there are no other instances of that object on the original thread. Value types are easy though, since they can be copied when passed across. __gshared _might_ be the best choice in your case. However, usually, it's just the quick and easy way out that gets things working but which risks subtle bugs down the line. Personally, I would only ever use it when integrating with a C or C++ library. - Jonathan M Davis
Jul 21 2016