473,320 Members | 1,862 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,320 software developers and data experts.

Is pass by reference thread safe?

Hi,
If I have this function

void DoSomething(int& index)
{
::Sleep(10000);
DoSomethingWith(index);
}

Is the variable index thread safe? Meaning that if I call this from two
different threads, would each thread have its own copy of index?

Thanks
Jul 23 '05 #1
9 5575
Andy Chang wrote:
Hi,
If I have this function

void DoSomething(int& index)
{
::Sleep(10000);
DoSomethingWith(index);
}

Is the variable index thread safe? Meaning that if I call this from two
different threads, would each thread have its own copy of index?

Stock answer: C++ has no concepts of threads, so your question is not
topical here.

Past that, think about it logically: if multiple threads of execution
refer to the same variable (as is the case here; remember, the whole
point of references is that they are aliases to already existing objects).

HTH,
--ag
--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays
Jul 23 '05 #2
Andy Chang wrote:
Hi,
If I have this function

void DoSomething(int& index)
{
::Sleep(10000);
DoSomethingWith(index);
}

Is the variable index thread safe? Meaning that if I call this from two
different threads, would each thread have its own copy of index?

Stock answer: C++ has no concepts of threads, so your question is not
topical here.

Past that, think about it logically: If multiple threads of execution
refer to the same variable (as is the case here; remember, the whole
point of references is that they are aliases to already existing
objects), thread safety is determined by what you're doing in, say,
DoSomethingWith(), etc.

HTH,
--ag
--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays
Jul 23 '05 #3
Andy Chang wrote:
Hi,
If I have this function

void DoSomething(int& index)
{
::Sleep(10000);

This has not much to do with thread safety itself.

DoSomethingWith(index);
}

Is the variable index thread safe? Meaning that if I call this from two
different threads, would each thread have its own copy of index?

Threads are off topic in clc++ since so far it is a system-specific
feature and not part of ISO C++. You will get more help in a newsgroup
about your platform.
In a lock based thread environment, and depending on how you have setup
your threads, above can be thread safe.
An example of the .NET multithreading model:
// .NET multithreading thread-lock model
__gc class SomeClass
{
int index;

//...

public:

// ...
void DoSomething()
{
Monitor::Enter(this);

// Modify index

Monitor::Exit();
}

void DoSomethingElse()
{
Monitor::Enter(this);

// Modify index

Monitor::Exit();
}

// ...
};
SomeClass *ps= __gc new SomeClass;

// ...

Thread *pthread1= __gc new Thread ( __gc new ThreadStart(ps,
&SomeClass::DoSomething) );

Thread *pthread2= __gc new Thread ( __gc new ThreadStart(ps,
&SomeClass::DoSomethingElse) );
//Start execution of ps->DoSomething()
pthread1->Start();

//Start execution of ps->DoSomethingElse()
pthread2->Start();

// ...

I have no experience with any other thread-style models or
multithreading in procedural paradigm.
But in a procedural lock-based model, I can guess you will do something
like this in your function:
void DoSomething(int& index)
{
Monitor::Enter(SOMETHING); // Or some lock function

// Modify index

Monitor::Exit(); // Or some unlock function
}

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #4
Suppose DoSomething(int& index) is called with different indexes as in

DoSomething(giThreadAIndex) and DoSomething(giThreadBIndex) from two
threads, then the function is thread safe even without the monitor calls? I
mean there is no possibility that the call from thread A could modify thread
B's index and vice versa?

Sorry, I didn't know which group to post and this is rather important point
I need to clear out.

Thanks

"Ioannis Vranos" <iv*@remove.this.grad.com> wrote in message
news:1109734925.342146@athnrd02...
Andy Chang wrote:
Hi,
If I have this function

void DoSomething(int& index)
{
::Sleep(10000);

This has not much to do with thread safety itself.

DoSomethingWith(index);
}

Is the variable index thread safe? Meaning that if I call this from
two different threads, would each thread have its own copy of index?

Threads are off topic in clc++ since so far it is a system-specific
feature and not part of ISO C++. You will get more help in a newsgroup
about your platform.
In a lock based thread environment, and depending on how you have setup
your threads, above can be thread safe.
An example of the .NET multithreading model:
// .NET multithreading thread-lock model
__gc class SomeClass
{
int index;

//...

public:

// ...
void DoSomething()
{
Monitor::Enter(this);

// Modify index

Monitor::Exit();
}

void DoSomethingElse()
{
Monitor::Enter(this);

// Modify index

Monitor::Exit();
}

// ...
};
SomeClass *ps= __gc new SomeClass;

// ...

Thread *pthread1= __gc new Thread ( __gc new ThreadStart(ps,
&SomeClass::DoSomething) );

Thread *pthread2= __gc new Thread ( __gc new ThreadStart(ps,
&SomeClass::DoSomethingElse) );
//Start execution of ps->DoSomething()
pthread1->Start();

//Start execution of ps->DoSomethingElse()
pthread2->Start();

// ...

I have no experience with any other thread-style models or multithreading
in procedural paradigm.
But in a procedural lock-based model, I can guess you will do something
like this in your function:
void DoSomething(int& index)
{
Monitor::Enter(SOMETHING); // Or some lock function

// Modify index

Monitor::Exit(); // Or some unlock function
}

--
Ioannis Vranos

http://www23.brinkster.com/noicys

Jul 23 '05 #5
Andy Chang wrote:
Suppose DoSomething(int& index) is called with different indexes as in

DoSomething(giThreadAIndex) and DoSomething(giThreadBIndex) from two
threads, then the function is thread safe even without the monitor
calls? I mean there is no possibility that the call from thread A
could modify thread B's index and vice versa?
As others have said, threads are OT. However, I think it is safe to say that
different threads executing DoSomething at the same time will have different
copies of local variables, including index. So Thread B could conceivably modify
the int referred to by thread A's copy of index during the execution of
DoSomething, but not via its own copy of index unless Thread A's index and
Thread B's happen to refer to the same int.
Sorry, I didn't know which group to post and this is rather important
point I need to clear out.
Try comp.programming.threads
Thanks


Jonathan
Jul 23 '05 #6
Andy Chang wrote:
Suppose DoSomething(int& index) is called with different indexes as in

DoSomething(giThreadAIndex) and DoSomething(giThreadBIndex) from two
threads, then the function is thread safe even without the monitor calls? I
mean there is no possibility that the call from thread A could modify thread
B's index and vice versa?
Yes, if the arguments are not related, then the operations are thread safe.

Sorry, I didn't know which group to post and this is rather important point
I need to clear out.

Which is your platform?

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #7
Ioannis Vranos wrote:
Yes, if the arguments are not related, then the operations
on them
are thread safe.


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #8
Andy Chang wrote:
Hi,
If I have this function

void DoSomething(int& index)
{
::Sleep(10000);
DoSomethingWith(index);
}

Is the variable index thread safe? Meaning that if I call this from two
different threads, would each thread have its own copy of index?


It depends on where index is referencing.

// this below would unquestionably thread safe.
void DoSomething(int index)
{
::Sleep(10000);
DoSomethingWith(index);
}

However, since, in your example, index is passed in as a non-const
reference, it's impossible to tell what it referencing and what
DoSomethingWith(index) is going to do with it.
Jul 23 '05 #9
Andy Chang wrote:
If I have this function:

void DoSomething(int& index) {
::Sleep(10000);
DoSomethingWith(index);
}

Is the variable index thread safe?
Meaning that if I call this from two different threads,
would each thread have its own copy of index?


You are confused.
A *variable* is never "thread safe".
Your *code* "thread safe"
only if two different threads
cannot modify the same variable.
If you pass a non-const reference [index]
to an int variable shared by two or more threads,
your code is *not* "thread safe"
since the non-const reference implies that
the variable can be modified.

Please note that
people sometimes refer to code that is actually threaded --
code that employs mutual exclusion explicitly
to protect shared variables -- as "thread safe".
Jul 23 '05 #10

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

Similar topics

0
by: Timo | last post by:
I'm trying to make a thread safe object cache without locking. The objects are cached by the id of the data dict given in __new__. Objects are removed from the cache as soon as they are no longer...
6
by: GG | last post by:
Is this public static method thread safe. //Receives a file name as a parameter //and returns the contents of that file as a string public static string FileToStr(string fileName) { FileStream...
4
by: Chris Newby | last post by:
When accessing, for example, an object stored in the session such as: Session.MyProperty = "Some Value"; Is access to MyObject thread-safe?
7
by: Alexander Walker | last post by:
Hello I want to get the value of a property of a control from a thread other than the thread the control was created on, as far as I can see this is not the same as invoking an operation on a...
1
by: Macca | last post by:
Hi I have a N-tier ASP.NET application that uses a data access tier to get data from a database and pass it to the middleware/business tier for processing/filtering and then passes the modified...
2
by: lwhitb1 | last post by:
Does anyone have any input on setting up my CAB application so that the application is thread safe, and cached appropiately? I read that this can be managed through Services, and dynamic injection....
3
by: tcomer | last post by:
Hello! I'm working on an asynchronous network application that uses multiple threads to do it's work. I have a ChatClient class that handles the basic functionality of connecting to a server and...
15
by: Laser Lu | last post by:
I was often noted by Thread Safety declarations when I was reading .NET Framework Class Library documents in MSDN. The declaration is usually described as 'Any public static (Shared in Visual...
3
by: andreas.zetterstrom | last post by:
I'm implementing some different c++ classes which I want to be thread safe. All these classes contain lists or arrays of some kind. I'm using protected sections to make them thread safe. The...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.