Mathematical path -vb.net parallel computing (2), -vb.net parallel computing

Source: Internet
Author: User

Mathematical path -vb.net parallel computing (2), -vb.net parallel computing

I. TLS types

1) Dynamic TLS

2) static TLS

Static TLS is faster than dynamic TLS. It is determined during the compilation period that a static domain must be defined to represent TLS data. the compiler has enough information to transmit code during the compilation period, dynamic TLS requires one or more function calls to obtain the address.

Ii. static TLS

We can declare a static variable using static in the thread function, which will be shared by all threads that use this function. For example, we write a simple calculation:

200-1-2-....-20

The subtraction part is completed by three threads, which means that the three threads need to share a temporary calculation result.

Imports System
Imports System. Threading




Module Module1


Sub Main ()


Dim mythread1 As Thread
Dim mythread2 As Thread
Dim mythread3 As Thread
'Create a thread object
Mythread1 = New Thread (AddressOf mythreadrun)
Mythread2 = New Thread (AddressOf mythreadrun)
Mythread3 = New Thread (AddressOf mythreadrun)
Console. WriteLine (Now. ToLongTimeString & "after the thread object is created, start the execution thread ")


'Execution thread
Mythread1.Start ("thread 1 ")
Mythread2.Start ("thread 2 ")
Mythread3.Start ("thread 3 ")
'Wait for the thread to finish
Mythread1.Join ()
Mythread2.Join ()
Mythread3.Join ()
'Thread execution completed
Console. WriteLine (Now. ToLongTimeString & "finished thread execution! ")
End Sub
Public Sub mythreadrun (ByVal data As Object)
Dim mynum As Integer
Static jg As Integer = 200
Dim temp As Integer
Try
For mynum = 1 To 20
Temp = jg
Jg-= mynum
Console. WriteLine (data & ":" & Now. ToLongTimeString & "=>" & temp & "-" & mynum & ", the calculation result is:" & jg)
Thread. Sleep (1)
Next
Catch
Console. WriteLine (data & ":" & Now. ToLongTimeString & "thread termination exception! ")
'Terminate the thread
Thread. CurrentThread. Abort ()
End Try
End Sub
End Module

Dim mynum As Integer
Static jg As Integer = 200
Dim temp As Integer
Try
For mynum = 1 To 20
Temp = jg
Jg-= mynum
Console. WriteLine (data & ":" & Now. ToLongTimeString & "=>" & temp & "-" & mynum & ", the calculation result is:" & jg)
Thread. Sleep (1)
Next

 

Jg is a static domain shared by multiple threads.

The running result is as follows:



 

 

We use the static TLS function to make the jg a local variable of the thread. For each thread

Operate the copy of this variable

 

In some multithreading solutions, each thread may have to provide its own private data. This type of data is called "local thread data ". In. NET Framework 3.5 and earlier versions, you can apply the ThreadStatic feature to static variables to make them local variables of the thread. However, using the ThreadStatic feature can cause minor errors. For example, even if the basic initialization statement causes the variable to be initialized only on the first thread accessing it, as shown in the following example:

<ThreadStaticAttribute> _
Shared counter As Integer

For example:

Imports System
Imports System. Threading


Class Test


<MTAThread> _
Shared Sub Main ()


For I As Integer = 1 To 3
Dim newThread As New Thread (AddressOf ThreadData. ThreadStaticDemo)
NewThread. Start ()
Next I


End Sub


End Class


Class ThreadData


<ThreadStaticAttribute> _
Shared threadSpecificData As Integer


Shared Sub ThreadStaticDemo ()


'Store the managed thread id for each thread in the static
'Variable.
ThreadSpecificData = Thread. CurrentThread. ManagedThreadId


'Allow other threads time to execute the same code, to show
'That the static data is unique to each thread.
Thread. Sleep (1000)


'Display the static data.
Console. WriteLine ("Data for managed thread {0 }:{ 1 }",_
Thread. CurrentThread. ManagedThreadId, threadSpecificData)


End Sub


End Class


'This code example produces output similar to the following:
'
'Data for managed thread 4: 4
'Data for managed thread 5: 5
'Data for managed thread 3: 3

On all other threads, the variable is initialized by using the default value (zero.

 

All content of this blog is original, if reproduced please indicate the source http://blog.csdn.net/myhaspl/
We use an alternative solution in. net 4.0, that is, vb.net 2010:

, You can use System. threading. threadLocal (Of T) type creates instance-based thread local variables, which can be initialized on all threads through the Action (Of T) delegate you provide.

Imports System
Imports System. Threading




Module Module1


Sub Main ()


Dim mythread1 As Thread
Dim mythread2 As Thread
Dim mythread3 As Thread
'Create a thread object
Mythread1 = New Thread (AddressOf mythreadrun)
Mythread2 = New Thread (AddressOf mythreadrun)
Mythread3 = New Thread (AddressOf mythreadrun)
Console. WriteLine (Now. ToLongTimeString & "after the thread object is created, start the execution thread ")


'Execution thread
Mythread1.Start ("thread 1 ")
Mythread2.Start ("thread 2 ")
Mythread3.Start ("thread 3 ")
'Wait for the thread to finish
Mythread1.Join ()
Mythread2.Join ()
Mythread3.Join ()
'Thread execution completed
Console. WriteLine (Now. ToLongTimeString & "finished thread execution! ")
End Sub
Public Sub mythreadrun (ByVal data As Object)
Dim mynum As Integer
Dim jg As ThreadLocal (Of Integer) = New ThreadLocal (Of Integer) (Function () 50)
Try
For mynum = 1 To 5
Jg. Value-= mynum
Console. writeLine (data & "& Now. toLongTimeString & "=>" & (jg. value + mynum) & "-" & mynum & ", the calculation result is:" & jg. value)
Thread. Sleep (2)
Next
Catch
Console. WriteLine (data & "& Now. ToLongTimeString &" thread terminated abnormally! ")
'Terminate the thread
Thread. CurrentThread. Abort ()
End Try
End Sub

End Module




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.