Click here to Skip to main content
15,892,697 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

i have three functions in a module that several threads would be using. all of the functions access local variable except the main doWork sub.

Sub DoWork(byval i as integer)

synclock (ListTasks)
dim strItem  as string =ListTasks(CInt(i)).ToString
end SyncLock

dim strHtml as string = GetHtml(strItem )
dim strParsed as string = ParseHtml(strHtml)
dim strResult as string = Report(strParsed )
End sub

Function GetHtml(byval url as string) as string
'code to get website
ens sub

Function ParseHtml(Byval html as string) as string
'code to parse HtmlString
end function

Function Report(Byval html as string) as string
'do the work
end function


Is this a thread safe code?
Posted
Updated 24-Jun-11 9:16am
v3
Comments
wizardzz 24-Jun-11 15:00pm    
Removed the C# tag

Have a read of this CP article Introduction to making multithreaded VB.NET Apps[^]
 
Share this answer
 
Let me explain it.

Functions don't matter, the threads do. I mean, the same function can be called in different threads. What the function does is thread-safe in one case and not thread-safe in another.

Therefore, forger about functions and focus and threads and what resources are accessed. If more then one thread access the same resource, the access code should be locked out using some lock mechanism. In this case, the code itself can be the same called from different thread, of different; it does not matter; it's only important what is accessed by this code.

Locking is not the only thing. Calls can be serialized for example, by putting the delegates (code) or data in a queue. For example, see my short Tips/Trick article here: Simple Blocking Queue for Thread Communication and Inter-thread Invocation[^].

—SA
 
Share this answer
 
No.
 
Share this answer
 
Comments
Cool Smith 24-Jun-11 15:55pm    
could you give any reason?
#realJSOP 24-Jun-11 18:02pm    
Think about it. Multiple threads accessing the same methods and variables with no control over when a thread can read/write the data or when a method can even process anything. No. Your code is not "thread-safe".

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900