By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,587 Members | 1,070 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,587 IT Pros & Developers. It's quick & easy.

1 function seen as 2

P: n/a
I have many module cpp with many functions.
I need to call these functions from 2 threads.
I'd like to do something to tell the compiler that each thread has its own
functions. The easiest way is to write the same functions twice, but there
are many functions.
Is there an "automatic" way to do that?

Thanks
Cristiano

Aug 14 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Cristiano wrote:
I have many module cpp with many functions.
I need to call these functions from 2 threads.
I'd like to do something to tell the compiler that each thread has its own
functions. The easiest way is to write the same functions twice, but there
are many functions.
Is there an "automatic" way to do that?

Thanks
Cristiano
Not sure if this is what your after, but look up "thread local storage"

It will be specific to your platform, so you are probably better asking
this question in a windows/linux/other* development newsgroup

Glyn
Aug 14 '07 #2

P: n/a
Hi!

Cristiano schrieb:
I have many module cpp with many functions.
I need to call these functions from 2 threads.
I'd like to do something to tell the compiler that each thread has its own
functions.
Do the functions use global variables? Does the word "static" occur in
your functions? If not then you may probably call the same function from
different threads.

Example:

int lastError; //global variable, BAD
void setLastError(int error)
{
lastError = error; //not thread safe
}

char* makeString(int i)
{
static char result[5] = {0}; //like global data

result[0] = i<5 ? 'A' : 'B'; //not thread safe
return result;
}
//thread safe:
std::string giveName(int user)
{
switch(user)
{
case 42: return "John";
default:
}
return "unknown user";
}

Frank
Aug 14 '07 #3

P: n/a
Frank Birbacher wrote:
Cristiano schrieb:
>I have many module cpp with many functions.
I need to call these functions from 2 threads.
I'd like to do something to tell the compiler that each thread has
its own functions.

Do the functions use global variables? Does the word "static" occur in
your functions?
No, but I didn't write the code with the shared functions, so I don't know
the code very well. I wrote the code which uses the shared functions.
If not then you may probably call the same function from different
threads.
I already seen that my application crashes in multi-thread mode. There are
many shared functions with parameter passing (is an FFT library) and the
code "as is" cannot work.
Example:
[...]
char* makeString(int i)
{
static char result[5] = {0}; //like global data

result[0] = i<5 ? 'A' : 'B'; //not thread safe
return result;
}
That fails also without the static result[] when changed this way:

char* makeString(int i)
{
char result[5] = {0};
result[0] = i<5 ? 'A' : 'B'; //not thread safe

// Do something with i and/or result

// When the thread 1 is here and the thread 2 calls
// this function i and result are corrupted :-(

return result;
}

All the functions are like that.

Thank you
Cristiano

Aug 14 '07 #4

P: n/a
"Cristiano" <cr**********@NSquipo.itwrote in message
news:46***********************@reader5.news.tin.it ...
>I have many module cpp with many functions.
I need to call these functions from 2 threads.
I'd like to do something to tell the compiler that each thread has its own
functions. The easiest way is to write the same functions twice, but there
are many functions.
Is there an "automatic" way to do that?
If you inline the function the compiler may inline it. But I suspect your
question has more to deal with static variables in the functions, otherwise
it wouldn't be an issue.

One way you could cheat would be to make the function a template and pass
different types from each thread, but that is at least a kludge, if not a
bad one.

Why not make the function the method of a class, and have each thread
instantize it's own instance.
Aug 15 '07 #5

P: n/a
Alf P. Steinbach wrote:
* Glyn Davies:
>Cristiano wrote:
>>I have many module cpp with many functions.
I need to call these functions from 2 threads.
I'd like to do something to tell the compiler that each thread has
its own
functions. The easiest way is to write the same functions twice, but
there
are many functions.
Is there an "automatic" way to do that?

Thanks
Cristiano

Not sure if this is what your after, but look up "thread local storage"

It will be specific to your platform, so you are probably better
asking this question in a windows/linux/other* development newsgroup

Boost threads offer portable thread local storage.
Handy to know - ta

I've only really recently started making use of Boost

Glyn
Aug 15 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.