In a single threaded app, I have some functions with static data so
that they can retain thjeir value between invocations. I.E.
int fn ( )
{
static int value;
int temp_value = value;
value = //calculate some new value
return temp_value;
}
Now I want to make this function thread safe. Is there a graceful way
to allocate static data on a per thread basis?
In a single threaded app, I have some functions with static data so
that they can retain thjeir value between invocations. I.E.
int fn ( )
{
static int value;
int temp_value = value;
value = //calculate some new value
return temp_value;
}
Now I want to make this function thread safe. Is there a graceful way
to allocate static data on a per thread basis?
POSIX keys are one method; see pthread_key*() and pthread_*specific()
in the reference, or the thread fingerprint utility in the book
Cheers,
-RK
–
[If replying via email, you’ll need to click on the URL that’s emailed to you
afterwards to forward the email to me – spam filters and all that]
Robert Krten, PDP minicomputer collector http://www.parse.com/~pdp8/
In a single threaded app, I have some functions with static data so
that they can retain thjeir value between invocations. I.E.
int fn ( )
{
static int value;
int temp_value = value;
value = //calculate some new value
return temp_value;
}
Now I want to make this function thread safe. Is there a graceful way
to allocate static data on a per thread basis?
RK > POSIX keys are one method; see pthread_key*() and pthread_*specific()
RK > in the reference, or the thread fingerprint utility in the book
I guess I was hoping for something with a little less overhead. I had
implemented something where there was a static array of data objects.
The function uses the n’th element of the array where n is the thread
number. The size of the array is the arbitrary maximum thread number
that you want to support.
I know that is isn’t POSIX. I was hoping there was something as
efficient that was. I was also hoping for a mether that would get
around the predetermined size of the array.
I.E.
int fn ( )
{
static int value_array [MAX_THREAD];
int & value = value [ ptyhread_self() ]
int temp_value = value;
value = //calculate some new value
return temp_value;
}
Robert Krten <> rk@parse.com> > wrote:
RK > Bill Caroselli <> qtps@earthlink.net> > wrote:
In a single threaded app, I have some functions with static data so
that they can retain thjeir value between invocations. I.E.
int fn ( )
{
static int value;
int temp_value = value;
value = //calculate some new value
return temp_value;
}
Now I want to make this function thread safe. Is there a graceful way
to allocate static data on a per thread basis?
RK > POSIX keys are one method; see pthread_key*() and pthread_*specific()
RK > in the reference, or the thread fingerprint utility in the book >
I guess I was hoping for something with a little less overhead.
Then you might want to reconsider your approach and redesign whole thing in a way to rid of statical
variables. Why don’t you want to keep track of values in a calling threads?
If you need an old value to calculate new value, you can define structure with old and new values
and pass pointer to this structure as parameter of fn(). In this case fn() can calculate new value
and do necessary move of structure members.
Merry Christmas!
Eduard
I had
implemented something where there was a static array of data objects.
The function uses the n’th element of the array where n is the thread
number. The size of the array is the arbitrary maximum thread number
that you want to support.
I know that is isn’t POSIX. I was hoping there was something as
efficient that was. I was also hoping for a mether that would get
around the predetermined size of the array.
I.E.
int fn ( )
{
static int value_array [MAX_THREAD];
int & value = value [ ptyhread_self() ]
int temp_value = value;
value = //calculate some new value
return temp_value;
}
Robert Krten <> rk@parse.com> > wrote:
RK > Bill Caroselli <> qtps@earthlink.net> > wrote:
In a single threaded app, I have some functions with static data so
that they can retain thjeir value between invocations. I.E.
int fn ( )
{
static int value;
int temp_value = value;
value = //calculate some new value
return temp_value;
}
Now I want to make this function thread safe. Is there a graceful way
to allocate static data on a per thread basis?
RK > POSIX keys are one method; see pthread_key*() and pthread_*specific()
RK > in the reference, or the thread fingerprint utility in the book >
I guess I was hoping for something with a little less overhead. I had
implemented something where there was a static array of data objects.
The function uses the n’th element of the array where n is the thread
number. The size of the array is the arbitrary maximum thread number
that you want to support.
It is non-portable, since thread ids are not required to be small integers.
It also won’t scale well if your threads are created/destroyed as you go.
I guess I was hoping for something with a little less overhead. I had
implemented something where there was a static array of data objects.
The function uses the n’th element of the array where n is the thread
number. The size of the array is the arbitrary maximum thread number
that you want to support.
IK > It is non-portable, since thread ids are not required to be small integers.
IK > It also won’t scale well if your threads are created/destroyed as you go.
Agreed.
Portability has never been an issue for me. I only work with QNX.
Robert Krten <> rk@parse.com> > wrote:
RK > Bill Caroselli <> qtps@earthlink.net> > wrote:
In a single threaded app, I have some functions with static data so
that they can retain thjeir value between invocations. I.E.
int fn ( )
{
static int value;
int temp_value = value;
value = //calculate some new value
return temp_value;
}
Now I want to make this function thread safe. Is there a graceful way
to allocate static data on a per thread basis?
RK > POSIX keys are one method; see pthread_key*() and pthread_*specific()
RK > in the reference, or the thread fingerprint utility in the book >
I guess I was hoping for something with a little less overhead. I had
implemented something where there was a static array of data objects.
The function uses the n’th element of the array where n is the thread
number. The size of the array is the arbitrary maximum thread number
that you want to support.
Um…the POSIX key stuff isn’t that bad. It is, essentially, a
2-dimensional sparse array, indexed by key and thread id.
If you only want to store an integer away, you can just store it
away, rather than storing an address of something.