473,396 Members | 1,891 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.

1 function seen as 2

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
5 1184
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
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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Gaurav Veda | last post by:
Hi ! I am a poor mortal who has become terrified of Python. It seems to have thrown all the OO concepts out of the window. Penniless, I ask a basic question : What is the difference between a...
9
by: Derek Hart | last post by:
I wish to execute code from a string. The string will have a function name, which will return a string: Dim a as string a = "MyFunctionName(param1, param2)" I have seen a ton of people...
39
by: Randell D. | last post by:
Folks, I'm sure this can be done legally, and not thru tricks of the trade - I hope someone can help. I'm writing a 'tool' (a function) which can be used generically in any of my projects. ...
13
by: Roy Hills | last post by:
I've seen two different function prototype formats used for ANSI C, and I'm unsure as to which is the correct or preferred one. 1st Format (this is what I use) type function(type, type, type);...
10
by: Xiaoshen Li | last post by:
Dear All, I am confused with prototypes in C. I saw the following code in a C book: void init_array_1(int data) { /* some code here */ }
5
by: Daz | last post by:
Hi everyone. My query is very straight forward (I think). What's the difference between someFunc.blah = function(){ ; } and
8
by: optimistx | last post by:
In excellent YAHOO user interface programs I see often extra parenthesis like this (foo=function(){ alert('I am foo'); })(); instead of bar=function(){
12
by: MonkeeSage | last post by:
Here's yet another take on a unique() function for sequences. It's more terse than others I've seen and works for all the common use cases (please report any errors on the recipe page): ...
6
by: smmk25 | last post by:
Before I state the problem, I just want to let the readers know, I am knew to C++\CLI and interop so please forgive any newbie questions. I have a huge C library which I want to be able to use in...
73
by: Steph Barklay | last post by:
Hi, I'm currently taking a data structures course in C, and my teacher said that function prototypes are not allowed in any of our code. He also said that no professional programmers use function...
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...

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.