local static data per thread

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?

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?

POSIX keys are one method; see pthread_key*() and pthread_*specific()
in the reference, or the thread fingerprint utility in the book :slight_smile:

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/

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 :slight_smile:

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;
}

In article <bscinr$402$1@inn.qnx.com>, qtps@earthlink.net says…

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 > :slight_smile:

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?

void new_thread( ){
int temp_value=0, value;
//
value=temp_value;
temp_value=fn();
//
}

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;
}

“Bill Caroselli” <qtps@earthlink.net> wrote in message
news:bscinr$402$1@inn.qnx.com

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 > :slight_smile:

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.

– igor

Igor Kovalenko <kovalenko@attbi.com> wrote:
IK > “Bill Caroselli” <qtps@earthlink.net> wrote in message

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.

Bill Caroselli <qtps@earthlink.net> wrote:

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 > :slight_smile:

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.

-David

QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.