473,834 Members | 2,298 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can someone explain Thread.Allocate DataSlot() method?

I was looking at the docs for Thread and can't understand the different
between "unnamed data slot "and "data slot".and docs in Thread.GetData( )
say "Threads use a local store memory mechanism to store thread-specific
data." what is "thread-specific data"?

Best regards
wmt

email:ia*****@h otmail.com

--
Message posted via http://www.dotnetmonster.com
Nov 22 '05 #1
6 2717
"iam1708 via DotNetMonster.c om" <fo***@nospam.D otNetMonster.co m> wrote in message news:<fa******* *************** ********@DotNet Monster.com>...
I was looking at the docs for Thread and can't understand the different
between "unnamed data slot "and "data slot".and docs in Thread.GetData( )
say "Threads use a local store memory mechanism to store thread-specific
data." what is "thread-specific data"?

Best regards
wmt

email:ia*****@h otmail.com


Hi

What you have read about is a Thread Local Storage (TLS). A TLS
variable is a variable that all the threads access in the same way,
but each of the thread can store a different value (each thread has
different copy). TLS makes it easy to write thread safe code. Each
thread has its own stack which means that the variables that declared
on the stack are not shared, but object and class member variables are
shared among all threads. Using TLS you can create non shared
non-stack based variables. In the days of Win32 when I was teaching
the TLS subject I used to give the C strtok() function example: The
function knows how to split a string by tokens. At first you call it
with two arguments, the token string and the start address of the
source string. To get the next word, you call it again, but now you
pass NULL as the address. The function remembers the last address in a
static variable. When you try to split two or more strings with two or
more threads concurrently, you find that all threads work on the same
(last) string. This happens because of the shared nature of the static
variable. Making this variable a TLS solves the problem. Each thread
gets its own copy of the last address state. In .Net there are three
ways to have a TLS:
1) An unnamed TLS, Using Thread.Allocate DataSlot(); the result (slot)
of this method should be shared among all thread. Each thread that
need a private storage uses the Thread.SetData( ) and Thread.GetData( )
passing it the shared slot.
2) A named TLS, Using AllocateNamedDa taSlot("Name") to create the
slot. Each thread uses the GetNamedDataSlo t() to get the slot and then
the Thread.SetData( ) and Thread.GetData( ) to work with the slot.
3) The [ThreadStaticAtt ribute], on a static variable.

The third option is the easiest one, you just decorate a class
variable and you get a TLS. The second is easier than the first
because you don't have to share a common slot between threads, but you
may collide with other code that uses the same name, so be careful.

If you need a TLS for a single class code, use the
ThreadStaticAtt ribute. If you need the TLS in many places (many
classes) use one of the Slot based methods. You may also consider the
call context mechanism, if you need the TLS state per call along the
call path.

You may also consider using an object instance per thread which omits
the need of TLS.

Alon Fliess
CTO
The Sela Group
Nov 22 '05 #2
Thanks a lot!but I have some question need help.
if the code is bellow ,it's allright.
//static int i;

public __gc class ThreadExample
{
[ThreadStatic]
static int i;

public:
static void ThreadProc()
{
i=0;
while (i<100)
{
printf("%d ,",i);
i++;
}
printf("**\n**" );
}
};

int _tmain()
{
Thread *oThread1 = new Thread(new ThreadStart(0,
&ThreadExample: :ThreadProc));
Thread *oThread2 = new Thread(new ThreadStart(0,
&ThreadExample: :ThreadProc));
oThread1->Start();
oThread2->Start();
Console::ReadLi ne();
return 0;
}
=============== =============== =====
but the code changed as bellow

static int i;

public __gc class ThreadExample
{
//[ThreadStatic]
//static int i;

public:
static void ThreadProc()
{
i=0;
while (i<100)
{
printf("%d ,",i);
i++;
}
printf("**\n**" );
}
};

int _tmain()
{
Thread *oThread1 = new Thread(new ThreadStart(0,
&ThreadExample: :ThreadProc));
Thread *oThread2 = new Thread(new ThreadStart(0,
&ThreadExample: :ThreadProc));
oThread1->Start();
oThread2->Start();
Console::ReadLi ne();
return 0;
}

the result is
(First time) "0 ,1 ,2 ,3 ,0 ,2 ,3 ,4 ,5 ,6 ,1 ,8 ,7 ,10 ,11 ,12 ,13 ,14 ,
15 ,16 ,........,99,"
(Second time)"0 ,0 ,1 ,2 ,3 ,......99,"
(Third time) "0 ,1 ,2 ,3 ,1 ,2 ,3 ,0 ,4 ,5 ,....99"
(Fourth time)"0 ,1 ,.....,98 ,99 ,91 "
even "0 ,1 ,2 ,......99 **\n** 0 ,1 ,2 ,.....99"

the result is some strange,because variable "i" is shared.from the result I
think their is anthoer copy of "i" in Cache.and in First time
"0 ,1,2,.....,0,2, ......,1,8,7... " ,but before the second "1" output ,"2"
output twice.Why?????

Best regards
wmt

--
Message posted via http://www.dotnetmonster.com
Nov 22 '05 #3
"iam1708 via DotNetMonster.c om" <fo***@DotNetMo nster.com> wrote in message news:<0a******* *************** ********@DotNet Monster.com>...
Thanks a lot!but I have some question need help.
if the code is bellow ,it's allright.
//static int i;

public __gc class ThreadExample
{
[ThreadStatic]
static int i;

public:
static void ThreadProc()
{
i=0;
while (i<100)
{
printf("%d ,",i);
i++;
}
printf("**\n**" );
}
};

int _tmain()
{
Thread *oThread1 = new Thread(new ThreadStart(0,
&ThreadExample: :ThreadProc));
Thread *oThread2 = new Thread(new ThreadStart(0,
&ThreadExample: :ThreadProc));
oThread1->Start();
oThread2->Start();
Console::ReadLi ne();
return 0;
}
=============== =============== =====
but the code changed as bellow

static int i;

public __gc class ThreadExample
{
//[ThreadStatic]
//static int i;

public:
static void ThreadProc()
{
i=0;
while (i<100)
{
printf("%d ,",i);
i++;
}
printf("**\n**" );
}
};

int _tmain()
{
Thread *oThread1 = new Thread(new ThreadStart(0,
&ThreadExample: :ThreadProc));
Thread *oThread2 = new Thread(new ThreadStart(0,
&ThreadExample: :ThreadProc));
oThread1->Start();
oThread2->Start();
Console::ReadLi ne();
return 0;
}

the result is
(First time) "0 ,1 ,2 ,3 ,0 ,2 ,3 ,4 ,5 ,6 ,1 ,8 ,7 ,10 ,11 ,12 ,13 ,14 ,
15 ,16 ,........,99,"
(Second time)"0 ,0 ,1 ,2 ,3 ,......99,"
(Third time) "0 ,1 ,2 ,3 ,1 ,2 ,3 ,0 ,4 ,5 ,....99"
(Fourth time)"0 ,1 ,.....,98 ,99 ,91 "
even "0 ,1 ,2 ,......99 **\n** 0 ,1 ,2 ,.....99"

the result is some strange,because variable "i" is shared.from the result I
think their is anthoer copy of "i" in Cache.and in First time
"0 ,1,2,.....,0,2, ......,1,8,7... " ,but before the second "1" output ,"2"
output twice.Why?????

Best regards
wmt


Hi

There are some potential problems with your test. Counting up to 100
takes less then a time-slice. you should spend some more time in the
loop ( Thread.Sleep() will do, or if you don't want to force context
switch, do some busy wait). If you use global static variable, make it
volatile (the compiler will not optimized it, and this may be the
problem that you see).
Printf uses buffers, it may not print immediate. Are you compiling
with multi-threaded C DLL, You may use System::Console instead printf,
it is thread safe in all cases.

Alon.
Nov 22 '05 #4
Thanks!
I instead "Console::write " of "printf" solve this porblem.

--
Message posted via http://www.dotnetmonster.com
Nov 22 '05 #5
I have some question need help
method Thread.Volatile Write() can write a value(example a ariable "i")in
Memory that is visible to all processors in the computer.
my question is:if the Cache of processors refresh immediate(auto) too?
through the example of below ,i believe not.but i can't sure.and if the
Cache of processor not refresh,How to refresh it?

public:
static void ThreadProc()
{
//i=0;
Thread::Volatil eWrite(&i, 0);
Console::Write( "*, ");
while (i<90)
{
Console::Write( i);
Console::Write( ", ");
i++;
}
Console::Write( i);
Console::Write( "+, ");
}
};
Best regards
wmt

--
Message posted via http://www.dotnetmonster.com
Nov 22 '05 #6
"iam1708 via DotNetMonster.c om" <fo***@DotNetMo nster.com> wrote in message news:<5d******* *************** ********@DotNet Monster.com>...
I have some question need help
method Thread.Volatile Write() can write a value(example a ariable "i")in
Memory that is visible to all processors in the computer.
my question is:if the Cache of processors refresh immediate(auto) too?
through the example of below ,i believe not.but i can't sure.and if the
Cache of processor not refresh,How to refresh it?

public:
static void ThreadProc()
{
//i=0;
Thread::Volatil eWrite(&i, 0);
Console::Write( "*, ");
while (i<90)
{
Console::Write( i);
Console::Write( ", ");
i++;
}
Console::Write( i);
Console::Write( "+, ");
}
};
Best regards
wmt


Hi again

Using the VolatileWrite/VolatileRead insure that the CPU refer to the
memory stored value and not a register or cache value (the same as the
volatile keyword). This means that you don't need to flush the CPU
cache to get the real coherent value. Maybe the problem is with your i
variable. Please add the full source code.

Alon.
Nov 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
453
by: iam1708 via DotNetMonster.com | last post by:
I was looking at the docs for Thread and can't understand the different between "unnamed data slot "and "data slot".and docs in Thread.GetData() say "Threads use a local store memory mechanism to store thread-specific data." what is "thread-specific data"? Best regards wmt email:iam1708@hotmail.com
6
1526
by: Zach | last post by:
Could someone please explain what the consequence of this //****** line is? public void SecondThread() { lock(m_smplQueue) { Monitor.Pulse(m_smplQueue); while(Monitor.Wait(m_smplQueue,1000))//****** {
4
1780
by: Zach | last post by:
(1.) What is the general meaning of the term 'callback'? (2.) What does 'callback' mean, as used in the context of threading? Many thanks, Zach.
14
2521
by: Robin Tucker | last post by:
Although I've been working on this project for 8 months now, I'm still not sure of the difference between ByVal and ByRef. As most objects in VB are reference types, passing ByVal I've discovered allows me to store a reference to the object I passed by val, to change that object and for the change to be reflected in the callers copy of the reference (confused?). Well, what is byref for in that case? I'm coming from a C++ background...
6
1193
by: rodchar | last post by:
hey all, i was wondering when you lookup commands in MSDN help in the description you'll often see it say that it's threadsafe or not. i know kinda what a thread is (needle and thread right? j/k :) no i really know a little about threads, but in lamen terms what does threadsafe mean? thanks, rodchar
3
2115
by: b.fokke | last post by:
The Thread class exposes AllocateNamedDataSlot() and AllocateDataSlot() methods which allow allocating memory for thread-specific data. How is this different from encapsulating each running thread in a class and using that class to store the data? Bram Fokke Utrecht, the Netherlands
3
1428
by: MagicKat | last post by:
Can you explain what they mean by "instant member" for instance, in the article about arraylists, it says it's thread safe but it's instant members are not. What does that mean? http://msdn2.microsoft.com/en-us/library/system.collections.arraylist.aspx
18
2271
by: Jon Slaughter | last post by:
"Instead of just waiting for its time slice to expire, a thread can block each time it initiates a time-consuming activity in another thread until the activity finishes. This is better than spinning in a polling loop waiting for completion because it allows other threads to run sooner than they would if the system had to rely solely on expiration of a time slice to turn its attention to some other thread." I don't get the "a thread...
18
10248
by: =?Utf-8?B?VGhlU2lsdmVySGFtbWVy?= | last post by:
Because C# has no native SSH class, I am using SharpSSH. Sometimes, for reasons I do not know, a Connect call will totally lock up the thread and never return. I am sure it has something to do with weirdness going on with the server I am talking to. Anyhow, this locked up state happens once in a while (maybe once per day) and I can't figure out how to deal with the locked up thread. If I issue a Thread.Abort() the exception never...
0
10786
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10503
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10544
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10214
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9326
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7754
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6951
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4425
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3079
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.