Thread: Multiple threads calling the same function at the same time

  1. #1
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    291

    Multiple threads calling the same function at the same time

    Hello, sorry for the title but I haven't find any more descriptive

    That's the question: can a function be called safely multiple times at the same moment? I'm using the Win32 API function 'CreateThread' on a server program to handle the incoming requests. The threaded function call several other functions to perform operations, but I'm afraid that in some way (I don't know in what way), the data managed by those other functions could be mixed. Is that a real problem? For the moment I haven't get any datamix, but can it be happen?

    Thank's in advance
    Niara

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Basically, there are no problems. Each function call has its own private set of local variables and will never interfere with other threads or other calls of the same function in the call stack.

    However, all resources that might be shared between threads are potentially dangerous. Static locals in functions, for example. Globals. Everything that is shared and mutable is a risk.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    It can happen if the function is not thread safe.
    For example if it uses some global data, has some static variables inside
    or receives as a parameter pointer so some data the same for each thread.

    In this case usage of static variables should be removed, usage of global and common data should be wrapped by some synchronization mechanism like Criticalsections
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    291
    Hi CornedBee and vart, thank's for your time and explanations.
    So I should understand that the o.s. will give to each function execution a part of memory to do it's work?

    I'm doing the next

    Code:
    ...
    //when the server accepts a connection, sends de socket as the parameter to the new thread
    s=accept(s,(sockaddr*)&client_descriptor,&client_descriptor_size);
    HANDLE tasca=CreateThread(0,0,ProcServei,(void*)s,0,&idTasca);
    if(tasca==NULL) {printf("Error creant tasca per la connexio entrant.\n");}
    CloseHandle(tasca);
    ...
    
    DWORD WINAPI ProcServei(void* sidx)
    {
    SOCKET sc=(SOCKET)sidx;
    workWithTheClient(sc);
    endTheConectionAndTheSocket(*s);
    return 0;
    }
    
    void workWithTheClient(SOCKET s)
    {
    REQUEST r;
    getClientRequest(&r);
    evalClientRequest(&r);
    freeRequest(&r);
    }
    
    bool getClientRequest(LPREQUEST r)
    {
    //receives the client request
    r.file=(char*)malloc(client_filerequest_size);
    if(!r.file) {return false;}
    //writes the file request to the struct value for it
    return true;
    }
    
    bool evalClientRequest(LPREQUEST r)
    {
    //validate the request, if is available, etc...
    //if necessary modifyes the request struct
    return true;
    }
    
    //the REQUEST struct is defined in an included file at the begin of the code
    typedef struct _REQUEST
    {
    char *file;
    }  REQUEST,*LPREQUEST;
    It's only a plan explanation, but my program functions are similar (and longer). My doublt is: the 'procServei()' function can be called several times (not at the same time, it depends on the incoming connections), but once that thread have been started, is possible that more that 1 thread calls at the same time the 'evalClientRequest()' function; I'm afraid that in certain moment (or with certain conditions), the data of the function called by thread #1 can be mixed with the data called by the thread #2 at the same time (or if not called at the same time, executed in coincident moment).

    Because I never have used threads (exept for practise), I'm not sure of how to handle that all.

    Thank's
    Niara

  5. #5
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    To solve this, your thread must be synchronized either by using mutex, semaphore, etc. Look here for further readings. I usually used a POSIX mutex (in Linux) for my threads. Don't know how to use it in Windows. Just be careful where to put your mutex locking/unlocking section cause it will affect the performance.
    Last edited by g4j31a5; 01-08-2007 at 12:22 AM.
    ERROR: Brain not found. Please insert a new brain!

    “Do nothing which is of no use.” - Miyamoto Musashi.

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    If each thread is working with the its own socket, and it is the only data, that is given to the thread by the main thread, and there is no global or static variables, I see no reason for mixing to occure

    All the local variables are created on stack when function is entered.
    So all local vars are created per thread.

    Like REQUEST r; is created when some thread enteres workWithTheClient function,
    when some other thread enteres this function another instance of the local var is created.

    Each thread then works with its own instance of the local variable, no mixing is possible.

    so why do you think the mixing can occure? From your code I don't see it is possible.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    291
    Hello, and thank's another time

    g4j31a5: I'll take a look to it, but I think that won't be necessary because the 'accept' function will avoid several calls to new processes creation at the same time no?

    vart: yes, each thread uses it's own data, there's no global nor static variables used in it. Well, I'm afraid of that because I thought that maybe there's some exceptional situations that it can be possible and mix some data. I don't know if that can be a problem, and never thought about it.

    I'm trying to implement a simple server side script to run with my test server. And now I have seen on the win32 manual reference on the section about mutex something important: sincronization is usefull when try to interact with a database, and that's what finally I would like to do. So I'll have to implement the thread call inheriting handles to a mutex object (or semaphores, or events, or timers) via SECURITY_ATTRIBUTES structure. Uf, it seems a lot of work... no, it seems a lot of difficult work

    Thank's
    Niara

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  4. The Timing is incorret
    By Drew in forum C++ Programming
    Replies: 5
    Last Post: 08-28-2003, 04:57 PM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM