473,396 Members | 1,914 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Can someone explain Thread.AllocateDataSlot() 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*****@hotmail.com

--
Message posted via http://www.dotnetmonster.com
Nov 22 '05 #1
6 2664
"iam1708 via DotNetMonster.com" <fo***@nospam.DotNetMonster.com> wrote in message news:<fa******************************@DotNetMonst er.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*****@hotmail.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.AllocateDataSlot(); 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 AllocateNamedDataSlot("Name") to create the
slot. Each thread uses the GetNamedDataSlot() to get the slot and then
the Thread.SetData() and Thread.GetData() to work with the slot.
3) The [ThreadStaticAttribute], 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
ThreadStaticAttribute. 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::ReadLine();
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::ReadLine();
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.com" <fo***@DotNetMonster.com> wrote in message news:<0a******************************@DotNetMonst er.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::ReadLine();
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::ReadLine();
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.VolatileWrite() 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::VolatileWrite(&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.com" <fo***@DotNetMonster.com> wrote in message news:<5d******************************@DotNetMonst er.com>...
I have some question need help
method Thread.VolatileWrite() 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::VolatileWrite(&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
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...
6
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);...
4
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
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...
6
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...
3
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...
3
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? ...
18
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...
18
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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,...
0
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...

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.