Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. DLL Function call

DLL Function call

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
15 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    ginjikun
    wrote on last edited by
    #1

    Hi everyone! Would like to ask... if i have a DLL that implements a function.. this DLL is used by an EXE. can the call to the DLL function by the EXE be done in parallel? say the EXE implements several thread and each of these threads calls the DLL function. or is the call just 1 at a time? Thanks for any help. not so newbie :)

    G C 2 Replies Last reply
    0
    • G ginjikun

      Hi everyone! Would like to ask... if i have a DLL that implements a function.. this DLL is used by an EXE. can the call to the DLL function by the EXE be done in parallel? say the EXE implements several thread and each of these threads calls the DLL function. or is the call just 1 at a time? Thanks for any help. not so newbie :)

      G Offline
      G Offline
      Gary R Wheeler
      wrote on last edited by
      #2

      The short answer is yes. Any number of threads can call a function in a DLL at the same time; the DLL calling mechanism allows this. The real answer is it depends on the function in the DLL. The DLL function must be designed to be thread-safe. In other words, you must know that the function can be called from multiple threads safely. If the function uses a global resource, it must protect accesses to that resource so that its state isn't changed incorrectly by multiple threads. If you don't know that the function is thread-safe, you can make it so by writing your own wrapper, something like this:

      static CCriticalSection MyFunction_CS;
      void Function(...);
      void MyFunction(...)
      {
      MyFunction_CS.Lock();
      Function(...);
      MyFunction_CS.Unlock();
      }

      Intead of calling Function(...) directly, you call MyFunction(...) which uses a critical section to ensure only a single thread at a time can call Function(...).

      Software Zen: delete this;
      Fold With Us![^]

      G 1 Reply Last reply
      0
      • G Gary R Wheeler

        The short answer is yes. Any number of threads can call a function in a DLL at the same time; the DLL calling mechanism allows this. The real answer is it depends on the function in the DLL. The DLL function must be designed to be thread-safe. In other words, you must know that the function can be called from multiple threads safely. If the function uses a global resource, it must protect accesses to that resource so that its state isn't changed incorrectly by multiple threads. If you don't know that the function is thread-safe, you can make it so by writing your own wrapper, something like this:

        static CCriticalSection MyFunction_CS;
        void Function(...);
        void MyFunction(...)
        {
        MyFunction_CS.Lock();
        Function(...);
        MyFunction_CS.Unlock();
        }

        Intead of calling Function(...) directly, you call MyFunction(...) which uses a critical section to ensure only a single thread at a time can call Function(...).

        Software Zen: delete this;
        Fold With Us![^]

        G Offline
        G Offline
        ginjikun
        wrote on last edited by
        #3

        Hi! thanks so much for the answer. if the function does not use global resoure and is something like below void Function(myobj *obj){ myobj* objTemp = new myobj; objTemp->a = obj->a; }; will there be a problem if the function is called by multiple threads? thanks again! not so newbie :)

        G 1 Reply Last reply
        0
        • G ginjikun

          Hi! thanks so much for the answer. if the function does not use global resoure and is something like below void Function(myobj *obj){ myobj* objTemp = new myobj; objTemp->a = obj->a; }; will there be a problem if the function is called by multiple threads? thanks again! not so newbie :)

          G Offline
          G Offline
          Gary R Wheeler
          wrote on last edited by
          #4

          This depends on the myobj class, and the type of member 'a'. If 'a' is a simple type (an int for example) then this code is probably thread-safe as it is. If it is something more complicated that has its own assignment operator, then this function might not be safe. Suppose a is a linked list of items allocated from a global pool. The assignment operator that performs the objTemp->a = obj->a operation would probably need to allocate items from the global pool. That might make this operation and this function not thread-safe.

          Software Zen: delete this;
          Fold With Us![^]

          G 1 Reply Last reply
          0
          • G Gary R Wheeler

            This depends on the myobj class, and the type of member 'a'. If 'a' is a simple type (an int for example) then this code is probably thread-safe as it is. If it is something more complicated that has its own assignment operator, then this function might not be safe. Suppose a is a linked list of items allocated from a global pool. The assignment operator that performs the objTemp->a = obj->a operation would probably need to allocate items from the global pool. That might make this operation and this function not thread-safe.

            Software Zen: delete this;
            Fold With Us![^]

            G Offline
            G Offline
            ginjikun
            wrote on last edited by
            #5

            sorry but what do you mean by global pool? thanks. not so newbie :)

            G 1 Reply Last reply
            0
            • G ginjikun

              Hi everyone! Would like to ask... if i have a DLL that implements a function.. this DLL is used by an EXE. can the call to the DLL function by the EXE be done in parallel? say the EXE implements several thread and each of these threads calls the DLL function. or is the call just 1 at a time? Thanks for any help. not so newbie :)

              C Offline
              C Offline
              CPallini
              wrote on last edited by
              #6

              You can call the DLL function in parallel, of course. But you have to be careful since your DLL function maybe not thread safe. :)

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              [my articles]

              G 1 Reply Last reply
              0
              • G ginjikun

                sorry but what do you mean by global pool? thanks. not so newbie :)

                G Offline
                G Offline
                Gary R Wheeler
                wrote on last edited by
                #7

                A common approach to keeping the size of a data structure limited is to allocate a fixed number of items and place them in a list of their own at the beginning of the program. This list is called the 'pool'. When the program needs a new item, it removes one from the pool. When it's done with the item, it puts it back in the pool. If the pool is ever empty when it needs a new item, it knows that the data structure has reached its size limit and it can react accordingly. This pool is a global resource. If you were to have multiple threads accessing it, thread 1 could be partway through removing an entry from the pool while thread 2 could attempt to add an entry to the pool. Pointers to the beginning and end of the pool could be set incorrectly. Later accesses might try to get an item from the pool that is really in use elsewhere, or items might be lost instead of being returned to the pool.

                Software Zen: delete this;
                Fold With Us![^]

                G 1 Reply Last reply
                0
                • G Gary R Wheeler

                  A common approach to keeping the size of a data structure limited is to allocate a fixed number of items and place them in a list of their own at the beginning of the program. This list is called the 'pool'. When the program needs a new item, it removes one from the pool. When it's done with the item, it puts it back in the pool. If the pool is ever empty when it needs a new item, it knows that the data structure has reached its size limit and it can react accordingly. This pool is a global resource. If you were to have multiple threads accessing it, thread 1 could be partway through removing an entry from the pool while thread 2 could attempt to add an entry to the pool. Pointers to the beginning and end of the pool could be set incorrectly. Later accesses might try to get an item from the pool that is really in use elsewhere, or items might be lost instead of being returned to the pool.

                  Software Zen: delete this;
                  Fold With Us![^]

                  G Offline
                  G Offline
                  ginjikun
                  wrote on last edited by
                  #8

                  im not sure if i understand what you meant by a the global resource... however the struct myobj as shown before is created everytime the function is called and sent as a data to a named pipe then the function returns. will there be a problem in that case? :)

                  G 1 Reply Last reply
                  0
                  • C CPallini

                    You can call the DLL function in parallel, of course. But you have to be careful since your DLL function maybe not thread safe. :)

                    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                    [my articles]

                    G Offline
                    G Offline
                    ginjikun
                    wrote on last edited by
                    #9

                    hi thanks for the answer.... now my problem is if i dont use a lock within the function... and the function is as shown below void function(myobj* obj) { myobj *objTemp = new myobj; objTemp->a = obj->a; // or if i use memcpy here send to namepipe... } will there be a problem if the function is called in parallel? thanks again. :)

                    C 1 Reply Last reply
                    0
                    • G ginjikun

                      im not sure if i understand what you meant by a the global resource... however the struct myobj as shown before is created everytime the function is called and sent as a data to a named pipe then the function returns. will there be a problem in that case? :)

                      G Offline
                      G Offline
                      Gary R Wheeler
                      wrote on last edited by
                      #10

                      A 'global resource' is an object in the program that will be accessed by more than one thread.

                      Software Zen: delete this;
                      Fold With Us![^]

                      G 1 Reply Last reply
                      0
                      • G ginjikun

                        hi thanks for the answer.... now my problem is if i dont use a lock within the function... and the function is as shown below void function(myobj* obj) { myobj *objTemp = new myobj; objTemp->a = obj->a; // or if i use memcpy here send to namepipe... } will there be a problem if the function is called in parallel? thanks again. :)

                        C Offline
                        C Offline
                        CPallini
                        wrote on last edited by
                        #11

                        You have to worry about shared resources, inside your function, as far shared resources are not used you're safe. You should try to figure out what happens if the thread executing the function loses the CPU possibly in favour of another thread running the same function: if such action is harmless, then you're safe (for instance if the named pipe is shared then a meaningless message can result if the threads write in parallel to) :)

                        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                        [my articles]

                        G 1 Reply Last reply
                        0
                        • C CPallini

                          You have to worry about shared resources, inside your function, as far shared resources are not used you're safe. You should try to figure out what happens if the thread executing the function loses the CPU possibly in favour of another thread running the same function: if such action is harmless, then you're safe (for instance if the named pipe is shared then a meaningless message can result if the threads write in parallel to) :)

                          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                          [my articles]

                          G Offline
                          G Offline
                          ginjikun
                          wrote on last edited by
                          #12

                          then my question next question will be if the named pipe is used only by that function.. however if that function is called in parallel causing 2 or more writing in the named pipe will that be a problem? or is it automatically handled by the named pipe that if there is currently a write in progress then other write functions will not interfere until it is finished? :)

                          I 1 Reply Last reply
                          0
                          • G Gary R Wheeler

                            A 'global resource' is an object in the program that will be accessed by more than one thread.

                            Software Zen: delete this;
                            Fold With Us![^]

                            G Offline
                            G Offline
                            ginjikun
                            wrote on last edited by
                            #13

                            does named pipe count? if the function is writing to the named pipe and the function is called in parallel will that be a problem? or does named pipe handle that scenario automatically? if not then i should only call the write function to the named pipe one at a time. :)

                            G 1 Reply Last reply
                            0
                            • G ginjikun

                              then my question next question will be if the named pipe is used only by that function.. however if that function is called in parallel causing 2 or more writing in the named pipe will that be a problem? or is it automatically handled by the named pipe that if there is currently a write in progress then other write functions will not interfere until it is finished? :)

                              I Offline
                              I Offline
                              Iain Clarke Warrior Programmer
                              wrote on last edited by
                              #14

                              CPalline wrote:

                              or instance if the named pipe is shared then a meaningless message can result if the threads write in parallel to

                              CPallini already answered that question for you. If you can write to the named pipe at the same time from multiple threads, then it doesn't matter if it would be the same function or different functions - It's still BAD. After all, the talking-to-the-named-pipe won't happen in MyFunctionA, or MyFunctionB - it will happen in ::WriteFile, which both of your functions can call. Iain.

                              Iain Clarke appearing by Special Request of CPallini.

                              1 Reply Last reply
                              0
                              • G ginjikun

                                does named pipe count? if the function is writing to the named pipe and the function is called in parallel will that be a problem? or does named pipe handle that scenario automatically? if not then i should only call the write function to the named pipe one at a time. :)

                                G Offline
                                G Offline
                                Gary R Wheeler
                                wrote on last edited by
                                #15

                                A named pipe certainly counts. I wouldn't think that named pipe access would be thread-safe. Proper thread safety depends on too many external factors. If you are using overlapped I/O, that changes things as well. The conservative approach would be to protect your write function with a critical section.

                                Software Zen: delete this;
                                Fold With Us![^]

                                1 Reply Last reply
                                0
                                Reply
                                • Reply as topic
                                Log in to reply
                                • Oldest to Newest
                                • Newest to Oldest
                                • Most Votes


                                • Login

                                • Don't have an account? Register

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • World
                                • Users
                                • Groups