Calling functions twice

I have a function which makes use of structures and some global variables. Briefly the function receives data from a comm port, processes it and sends a response. I would like to implement this function twice, i.e one listens to comm1 and responds through comm1, and the other listens on comm2 and responds on comm2. I pass two items to the function (rcx_buffer and commport to use). The problem is i have a structure for the function that stores certain status/values of the previous received frame, what i dont want is one call to the function (for say commport 1) to override globals that are required by another call to the function (for commport 2). What is the best way to acheive this or is this beyond C.
I could implement the function with 2 names and create a whole seperate list of globals, but this is messy.
I beleive i could change the globals to exist only in the scope of the function, would this work? But this wont easily work with the structure to the code i have created so far.

Any suggestions appreciated.


  • The only professional way to implement this is through two threads, one for each port. Each thread has its dedicated local variables containing status etc.

    If you don't use threads, your main graphics thread will lag and probably hang up in case of errors. This is a common fault of such programs.

    Threads will also solve your problem with global variables, since you can hide them inside each thread function, then ask the thread about their status in a protected way (with mutecies).
  • Thanks for the reply!
    I unfortunately wont be able to implement threads, its an embedded
    system with no RTOS. One idea i had was to contnue with the structure i have but use an Array of structures. To access the releveant structure in the array i will use the commport number that i am passing to the function, how does this sound? Any issues i should be aware of in using this?
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!