I still ask about thread synchronization

This is the forum for miscellaneous technical/programming questions.

Moderator: 2ffat

Re: I still ask about thread synchronization

Postby rlebeau » Wed Jun 19, 2019 11:30 am

mark_c wrote:if instead of using a single global variable msg I would use two global variables


The only thing that solves is avoiding one socket thread trampling over the strings of other client threads. You still have to sync access to the strings when passing them between threads, no matter how many strings you use.

Or, you could simply allocate a new string for each message you send to the UI, and let the main UI thread free the string when done using it. I showed you earlier how to do exactly that.

mark_c wrote:instead of using a single socket I would use two with two distinct ports


That is not necessary, or desirable. The more ports you open, the harder that makes server administration for network admins to deal with. Use fewer ports, not more ports.

mark_c wrote:even if not very elegant, in this way I would solve all the problems of race condition, right?


No.
Remy Lebeau (TeamB)
Lebeau Software
User avatar
rlebeau
BCBJ Author
BCBJ Author
 
Posts: 1610
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA

Re: I still ask about thread synchronization

Postby mark_c » Wed Jun 19, 2019 11:04 pm

thanks Remy.
Taking into account my buggy version of the code, the main problem that you Remy identified is the race condition caused by the global variable "msg": but I also ask you if you see deadlok problems, starvation or some unexpected block of the program.

Since the program does not perform any calculation, I do not see exceptions that can make it fail, is it correct?


curiosity:
from the "Embarcadero" documentation I read that all the components and events of the VCL are at the complete disposition of the main thread, it means that all the other threads that are implemented as secondary threads and that want to use methods or events of the main thread must always be synchronized, right?

I wonder: why did Borland not produce a completely thread safe development environment. An acquaintance of mine, told me that Python is completely thread safe, in practice, he doesn't synchronize anything when he develops code, Python does it, but I don't know if it's true.
mark_c
BCBJ Guru
BCBJ Guru
 
Posts: 170
Joined: Thu Jun 21, 2012 1:13 am

Re: I still ask about thread synchronization

Postby rlebeau » Fri Jun 21, 2019 12:05 pm

mark_c wrote:Taking into account my buggy version of the code, the main problem that you Remy identified is the race condition caused by the global variable "msg": but I also ask you if you see deadlok problems, starvation or some unexpected block of the program.

Since the program does not perform any calculation, I do not see exceptions that can make it fail, is it correct?


I already pointed out all of the problems I saw so far. As soon as you need to share data between threads, you have to think about synchronization.

mark_c wrote:from the "Embarcadero" documentation I read that all the components and events of the VCL are at the complete disposition of the main thread, it means that all the other threads that are implemented as secondary threads and that want to use methods or events of the main thread must always be synchronized, right?


Yes.

mark_c wrote:why did Borland not produce a completely thread safe development environment.


That is not how UI programing works on Windows. It wasn't their choice. Remember, the original VCL (that still exists today) is largely governed by the design and limitations of the Win32 API. UI elements have thread-specific affinity, they only work properly in the threads that create them. And since the VCL UI is almost always created in the main UI thread, then typically only the main UI thread can access the VCL directly.

mark_c wrote:An acquaintance of mine, told me that Python is completely thread safe, in practice, he doesn't synchronize anything when he develops code, Python does it, but I don't know if it's true.


I can't answer that, as I'm not a Python developer, I have never touched it. Different programming languages and different frameworks operate in different ways. Read Python's documentation and find out for yourself how it actually works.
Remy Lebeau (TeamB)
Lebeau Software
User avatar
rlebeau
BCBJ Author
BCBJ Author
 
Posts: 1610
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA

Previous

Return to Technical

Who is online

Users browsing this forum: No registered users and 9 guests