08-19-2023 08:42 AM
Greetings,
I have an application in LabWindows/CVI that is multi-threaded. I understand about using thread locks/semaphores around code to serialize writing of data and thereby protect data. My question goes deeper than that, in an effort to better understand when thread locks are necessary.
Let's say that I have a hypothetical function called "FormatTime" that takes an input parameter of time values and formats the date and time into a text string. Suppose that one thread calls this method, passing in a time data "A". In the middle of processing this method, the thread time-slice is frozen and execution passes to another thread that also calls this same method, passing in a different time value.
Question: Does the second thread execute a copy of the code, separate from the first thread, whereby the data values being processed are kept separate, or does the execution of the method by the second thread end up corrupting the data being processed by the execution of the method by the first thread?
Thanks in advance
Solved! Go to Solution.
08-20-2023 11:04 AM
It only depends on what variables are used. If the function accesses any global or static resource (variable, hardware device, or similar) you get likely a race condition. If however the function gets passed all necessary buffers as parameters that are allocated in the respective caller and/or only uses local stack variables, then each function will have its own variables to work on.
08-20-2023 04:47 PM
Thanks, Rolf. That's what I was thinking, but needed confirmation. I appreciate the help!