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. C++ code to create thread to handle method

C++ code to create thread to handle method

Scheduled Pinned Locked Moved C / C++ / MFC
c++testingbeta-testinghelpworkspace
13 Posts 5 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.
  • P pk jain

    Hi, i need to create multithreaded environment in my Unit-testing activity. For that i need to create multiple threads which can handle methods individully. And these mthods having different parameters & return types. So as per requirement i'm trying to create thread by using CreateThread().HEre the the thread can handle mehtod like in this format "DWORD WINAPI Function( LPVOID lpParam ) " Hence my questrion is that , how can we create thread using C++ to hadle method having 'different type of method parameter' & 'method return type' as well. Please help me soon. Thanks

    E Offline
    E Offline
    Erudite_Eric
    wrote on last edited by
    #4

    Sounds fairly basic to me, did you think of passing a context to each thread, with the context consisting of the function address to call and the parameters needed?

    1 Reply Last reply
    0
    • P pasztorpisti

      I can help you in threading because your problem is simple, I would do this with a task object queue (threadpool) but do you think you need multithreading for this? You are calling different methods of objects from different threads within the same process? You can test only threadsafe methods that don't interfere with each other this way. I would rather do this with multiprocessing instead of multithreading. You start several processes each of them performing a different test. This way each process has only one main test thread but these main/test threads dont interfere with each other because they are isolated in separate processes. Sometimes multiprocessing is the winner especially when you are using a non-threadsafe library. So why do you want to use multithreading?

      E Offline
      E Offline
      Erudite_Eric
      wrote on last edited by
      #5

      And these funcitos he is tesing are going to run in seperate processes are they? How likely is that? Perhaps they do share data and use mutexes to manage this and the entire POINT of testing is to make sure they work in the SAME process? Ever thought of that?

      P 1 Reply Last reply
      0
      • E Erudite_Eric

        And these funcitos he is tesing are going to run in seperate processes are they? How likely is that? Perhaps they do share data and use mutexes to manage this and the entire POINT of testing is to make sure they work in the SAME process? Ever thought of that?

        P Offline
        P Offline
        pasztorpisti
        wrote on last edited by
        #6

        Thanks again for your comments. Have you finished reading my post? The last question tries to find out the intentions of OP. Most of the well written modular multithreaded applications are built up from single threaded components containing the least possible locks and synchronization points between threads that work with separate instances of these single threaded modules. Since the components are usually single threaded most of the test cases are also single threaded and test the functions of a module. It's okay to create a few test cases to test the multithreaded interactions and synchronization points but this won't be the majority of the test cases. Parallelization of test cases within a single threaded component can be achieved safely only with multiprocessing. Actually I've worked on systems that were developed with lots of test cases and some of these systems were multithreaded and some of these were well designed at the same time.

        E 1 Reply Last reply
        0
        • P pasztorpisti

          Thanks again for your comments. Have you finished reading my post? The last question tries to find out the intentions of OP. Most of the well written modular multithreaded applications are built up from single threaded components containing the least possible locks and synchronization points between threads that work with separate instances of these single threaded modules. Since the components are usually single threaded most of the test cases are also single threaded and test the functions of a module. It's okay to create a few test cases to test the multithreaded interactions and synchronization points but this won't be the majority of the test cases. Parallelization of test cases within a single threaded component can be achieved safely only with multiprocessing. Actually I've worked on systems that were developed with lots of test cases and some of these systems were multithreaded and some of these were well designed at the same time.

          E Offline
          E Offline
          Erudite_Eric
          wrote on last edited by
          #7

          Fortunately opinion and fact are two different things.

          P 1 Reply Last reply
          0
          • E Erudite_Eric

            Fortunately opinion and fact are two different things.

            P Offline
            P Offline
            pasztorpisti
            wrote on last edited by
            #8

            If you think this then you have no clue about correct multithreading. BTW, returning to your first reply: have you read my first answer?

            E 1 Reply Last reply
            0
            • P pasztorpisti

              If you think this then you have no clue about correct multithreading. BTW, returning to your first reply: have you read my first answer?

              E Offline
              E Offline
              Erudite_Eric
              wrote on last edited by
              #9

              Fortunately opinion and fact are two different things.

              P 1 Reply Last reply
              0
              • E Erudite_Eric

                Fortunately opinion and fact are two different things.

                P Offline
                P Offline
                pasztorpisti
                wrote on last edited by
                #10

                If you think this then you have no clue about correct multithreading. BTW, returning to your first reply: have you read my first answer?

                E 1 Reply Last reply
                0
                • P pasztorpisti

                  If you think this then you have no clue about correct multithreading. BTW, returning to your first reply: have you read my first answer?

                  E Offline
                  E Offline
                  Erudite_Eric
                  wrote on last edited by
                  #11

                  I have done threadding that would make your eyes water.

                  P 1 Reply Last reply
                  0
                  • E Erudite_Eric

                    I have done threadding that would make your eyes water.

                    P Offline
                    P Offline
                    pasztorpisti
                    wrote on last edited by
                    #12

                    I'm not really interested in your threading. BTW, returning to your first reply: have you read my first answer?

                    1 Reply Last reply
                    0
                    • P pk jain

                      Hi, i need to create multithreaded environment in my Unit-testing activity. For that i need to create multiple threads which can handle methods individully. And these mthods having different parameters & return types. So as per requirement i'm trying to create thread by using CreateThread().HEre the the thread can handle mehtod like in this format "DWORD WINAPI Function( LPVOID lpParam ) " Hence my questrion is that , how can we create thread using C++ to hadle method having 'different type of method parameter' & 'method return type' as well. Please help me soon. Thanks

                      E Offline
                      E Offline
                      Eytukan
                      wrote on last edited by
                      #13

                      You don't need to change the param & return types of the thread function. leave them as it is. Just imagine the thread function as a platform to launch your test cases - which would be different functions. Just like the post above, I would choose to go with a data structure that takes in all the parameters required to execute the test case. Including the function pointer. If you are bothered about using function pointers you can even have a switch case block that directs the incoming structure-data to the right function using a normal function call. (it's not the best design I'd say, but it still works).

                      WORD WINAPI Function( LPVOID lpParam)
                      {
                      testcaseData ptcdata* = (testcaseData*) lpParam;
                      ptcData->Data1, data2.. etc

                      switch(ptcData->tcID)
                      {
                      case 0:
                      TestCase0(...)
                      case 1:

                      }

                      }

                      And you can use the same Test case index to fill in a Results-vector or list , where it updates the result of the particular test case. The vector can be global or be part of the struct & be protected by a critical section block inside the thread.

                      Starting to think people post kid pics in their profiles because that was the last time they were cute - Jeremy.

                      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