NeonGlow
NeonGlow

Reputation: 1692

Calling a function in C file from multiple threads

I am working with a code base with some .C files and .CPP files.

The multiple threads in the system calls some functions in C files as the one given below.

void CalculateCrc(PWORD pwCRC, PBYTE pbyBuffer, DWORD dwBufferLen)
{
    WORD wFCS = 0xffff;

    ASSERT(pbyBuffer);
    ASSERT(pwCRC);
    while(dwBufferLen--)
    {
        wFCS = (WORD)(wFCS >> 8) ^ tbl_FCS[(wFCS ^ *pbyBuffer++) & 0xff];
    }
    wFCS ^= 0xffff; // complement
    *pwCRC = wFCS;
}

For each calling thread will there be copies of arguments[pwCRC, pbyBuffer, dwBufferLen] and non-static data members of function [WORD wFCS], or will there be only a single set of data shared by all threads that will result in data corruption and make the calls from multiple threads unsafe?

I am not a native English speaker. Forgive me if the question is not asked in a clear manner.

Thanks for your time.

Upvotes: 1

Views: 1577

Answers (3)

luk32
luk32

Reputation: 16070

I believe each thread will have its own stack, which is a copy of the spawning process' stack (I hope I am technically correct on this one). They do share address space and heap though.

So anything that existed before spawn will be shared. Anything created after is thread-local. And since everything is passed by value, and data member is non-static, it will be created thread-local.

Your function per-se is safe. However, since you work with pointers, you need to take care that two threads do not work over the same memory area. The variables are safe, the memory is not.

Upvotes: 3

Rohan
Rohan

Reputation: 53326

will there be copies of arguments[pwCRC, pbyBuffer, dwBufferLen]

In C, the arguments are passed by value, so for each call from different threads these will have different copied. However, if the passed variables are global/shared by the threads then all such threads will pass the same variables.

In your case PWORD pwCRC, PBYTE pbyBuffer are pointers. If these are shared between the threads then also, your function is not thread-safe. As multiple threads may try to change the value pointed by these pointers.

non-static data members of function [WORD wFCS]

Yes, there will be copy for each function call.

Upvotes: 1

user3344003
user3344003

Reputation: 21627

The function will have its own copy of pwCRC and dwBufferLen BUT NOT pbyBuffer because you are passing it as a pointer.

I give two solutions:

A. ensure that all threads only have read (or no) access to pbyBuffer while this function is called; or (if the data is rather small

You could do this by making a copy.

B. Pass the buffer by value. you can do this by using a structure

struct buffer { char buffer [LEN] ; }

This only works if the buffer is small. If I remember correctly, the C++ standard limits the size of the call stack as a concession to the VAX architecture. Your compiler might exceed the limits of the standard. Even so, it is not a good idea to kill the stack with large arguments.

Upvotes: 1

Related Questions