Howdy, Stranger!

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

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Calling functions twice

mcompilemcompile Posts: 2Member
Hey
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.

Comments

  • LundinLundin Posts: 3,711Member
    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).
  • mcompilemcompile Posts: 2Member
    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?
    Thanks
Sign In or Register to comment.