Archived

This forum has been archived. Please start a new discussion on GitHub.

Static function variable in multithread applications

Hello, Ice Crew !

We obtained undefined behavior.

In the Ice - generated file we have (schematically):
"
::Ice::Int
IceDelegateM::InterfaceName::FuncName(Param_list)
{

static const ::std::string __operation("OperationName");
//etc.............
"

Under some condition (it is rather hard to reproduce) when 10 threads enter this function (first time) simultaneously we get next situation.
Only one thread has correctly initialized __operation variable and other threads have an empty string.
As a result we have next:
"
Ice exception : .\Outgoing.cpp:360: Ice::OperationNotExistException:
operation does not exist
"

Thank you!,
Alexey

P.S.
System:WinXP.
Ice Version: 2.0.0
Ice is used in DLL, which sits in the application (exe) directory.
We can reproduce this bug in debug configuration only and
at the first (exe) application running only.
I.e. if we run this application again we have not problems.
But if we replaced DLL (just copy from other directory) and run application we will have problem. Slightly source code changes remove this problem.

Comments

  • marc
    marc Florida
    I'm afraid from the description it's impossible to say what the problem is. Most likely, it's some kind of DLL mix-up, such as mixing debug with release library versions.

    If you would like us to investigate, please post a *small* self-contained example that demonstrates the problem. If you cannot reproduce the problem in a small example but would like us to have a look at your full application, then we can do this as commercial support. In this case, please contact us at info@zeroc.com.
  • Static function variables using in multithreaded applications.

    Hello again! :)
    Hm, we know our (and possible Ice ) problem.
    The problem is using static data in functions that are used in multi-threaded application. It is wrong (without any synchronization), sooner or later the problems will arise.
    For more info see:
    http://www.codesourcery.com/archives/cxx-abi-dev/msg00032.html

    Though, probably I incorrectly use some ice stuff.
    Can I setup Ice so as operations names (in generated from slice-definition to c++ sources) will not be static variables ?

    Thanks for your reply,:)
    Alexey.
  • marc
    marc Florida
    You are right, and removing these function-scope statics is actually on our todo list. We just didn't give this any priority yet, because in practice there has never been any problem with this so far. But apparently your application triggers this problem, so we better fix this. What C++ compiler version and patch level ("service pack") are you using?
  • I use service pack 2
    and build my projects by MSVC7.1. (MS Visual Studio.NET 2003)
    Ice's CRT's and my libraries are debug.

    >>But apparently your application triggers this problem, so we better fix this.
    I think, In any MT application there is some probability of such bug.
    For the time being I solved this problem by adding synchronization (at the first call) to each our function, which can be used by MT applications. :(
    Cause I wish that probability of this bug will not exist at all.

    BTW to solve this problem for you it is enough to replace
    static std::string __operation("OperationName");
    by smth like:
    static char * __operation = "OperationName";
    or better by:
    char * __operation = "OperationName";
    (I tested both variants and everything works well).

    Hm, may be there is some possibility to tune my compiler to avoid such problem...
    Alexey
  • I beg my pardon - realy string in Ice-generated file is:
    static const ::std::string __operation("OperationName");
  • marc
    marc Florida
    Using const char* is not so good, because then there will be additional conversion overhead at run-time, which are not good for performance. We will instead simply make them file-scope statics, which are thread-safe (because they are initialized by the main thread before any additional threads are started).
  • Yes. For memory and performance your suggestion well. :)
    Indeed in function calls would be overheads, (if the compiler is not very smart).
    I was afraid suggest it cause there is needed more changes for its implementation. ;)
    Oops, it seems I begin to spam.
    Thank you, again.
    do svidaniya. :)
  • Did this ever get addressed?

    I'm seeing some weird behaviour right now in multi-threaded C++ programs (using pthreads) on Windows, and I suspect it may have to do with this issue; it seems that the IDs are still static const etc. (Full disclosure: I haven't actually tried changing this yet.)

    This thread is from last year; did the changes discussed ever get made?

    Thanks,

    MEF
  • bernard
    bernard Jupiter, FL
    Hi Mary,

    Yes, this was fixed in Ice 3.0.

    Cheers,
    Bernard