Connecting Tech Pros Worldwide Forums | Help | Site Map

Multithreading problem

Boni
Guest
 
Posts: n/a
#1: Nov 21 '05
Dear all,
it seems that I have a multithreadng problem. I am not sure.
class A
UID as integer
shared ctr as integer=0
sub new()
UID=ctr
ctr+=1
end sub

end

Sometime I get 2 A objects with the same UID. I don't explicitely start
thread. Also it it possible that my class library is called from many
threads.
In order to find out, if it is really threading problem I need unique some
sort thread ID, which will be unique for each thread ?
But I can find only domainID in threading.thread.
Please is it possible to get some unique threadID or hashcode or something
else in .NET 1.1?



Ken Tucker [MVP]
Guest
 
Posts: n/a
#2: Nov 21 '05

re: Multithreading problem


Hi,

SyncLock prevents multiple threads from executing the same code at
the same time.

http://msdn.microsoft.com/library/de...tmsynclock.asp

Ken
---------------------------
"Boni" <oilia@nospam> wrote in message
news:OZgofYgsFHA.3216@TK2MSFTNGP12.phx.gbl...[color=blue]
> Dear all,
> it seems that I have a multithreadng problem. I am not sure.
> class A
> UID as integer
> shared ctr as integer=0
> sub new()
> UID=ctr
> ctr+=1
> end sub
>
> end
>
> Sometime I get 2 A objects with the same UID. I don't explicitely start
> thread. Also it it possible that my class library is called from many
> threads.
> In order to find out, if it is really threading problem I need unique some
> sort thread ID, which will be unique for each thread ?
> But I can find only domainID in threading.thread.
> Please is it possible to get some unique threadID or hashcode or something
> else in .NET 1.1?
>[/color]


Boni
Guest
 
Posts: n/a
#3: Nov 21 '05

re: Multithreading problem


Hi Ken
I know SyncLock. The problem is that I am not really sure if it is because
of multithreading. So I would like to output threadID or some other unique
thread info.
Is it possible?
Thanks,
Boni
"Ken Tucker [MVP]" <vb2ae@bellsouth.net> schrieb im Newsbeitrag
news:OBgGDsgsFHA.2948@TK2MSFTNGP15.phx.gbl...[color=blue]
> Hi,
>
> SyncLock prevents multiple threads from executing the same code at
> the same time.
>
> http://msdn.microsoft.com/library/de...tmsynclock.asp
>
> Ken
> ---------------------------
> "Boni" <oilia@nospam> wrote in message
> news:OZgofYgsFHA.3216@TK2MSFTNGP12.phx.gbl...[color=green]
>> Dear all,
>> it seems that I have a multithreadng problem. I am not sure.
>> class A
>> UID as integer
>> shared ctr as integer=0
>> sub new()
>> UID=ctr
>> ctr+=1
>> end sub
>>
>> end
>>
>> Sometime I get 2 A objects with the same UID. I don't explicitely start
>> thread. Also it it possible that my class library is called from many
>> threads.
>> In order to find out, if it is really threading problem I need unique
>> some sort thread ID, which will be unique for each thread ?
>> But I can find only domainID in threading.thread.
>> Please is it possible to get some unique threadID or hashcode or
>> something else in .NET 1.1?
>>[/color]
>
>[/color]


AMercer
Guest
 
Posts: n/a
#4: Nov 21 '05

re: Multithreading problem


To safely do what you want, try Threading.Interlocked.Increment().
Also, be advised that static applies to an AppDomain, a gotcha for the unwary.

david
Guest
 
Posts: n/a
#5: Nov 21 '05

re: Multithreading problem


On 2005-09-05, AMercer <AMercer@discussions.microsoft.com> wrote:[color=blue]
> To safely do what you want, try Threading.Interlocked.Increment().[/color]

That won't fix the problem, since it's possible for objects in different
threads to acquire the same UID previous to the atomic increment.

[color=blue]
> Also, be advised that static applies to an AppDomain, a gotcha for the unwary.
>[/color]
AMercer
Guest
 
Posts: n/a
#6: Nov 21 '05

re: Multithreading problem


> That won't fix the problem, since it's possible for objects in different[color=blue]
> threads to acquire the same UID previous to the atomic increment.[/color]

in the constructor,
uid=Threading.Interlocked.Increment(ctr)
gets the job done for all threads in the same appdomain.

david
Guest
 
Posts: n/a
#7: Nov 21 '05

re: Multithreading problem


On 2005-09-05, AMercer <AMercer@discussions.microsoft.com> wrote:[color=blue][color=green]
>> That won't fix the problem, since it's possible for objects in different
>> threads to acquire the same UID previous to the atomic increment.[/color]
>
> in the constructor,
> uid=Threading.Interlocked.Increment(ctr)
> gets the job done for all threads in the same appdomain.[/color]

You're right of course. Somehow I got focused on the fact that the
assignment was on a different line in the original post and commented
on that.

Closed Thread


Similar Visual Basic .NET bytes