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. Passing Variables to Thread Process [modified]

Passing Variables to Thread Process [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorial
14 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.
  • D Offline
    D Offline
    darkcloud 42o
    wrote on last edited by
    #1

    //hi the type of threading im using is.. #include #include using namespace std; int shutdown=0; int main(){ unsigned sid; (HANDLE)_beginthreadex( NULL, 0, threadprocess, NULL, 0, &sid); while(1){ Sleep(1000); if(shutdown==1){ break; } } } unsigned __stdcall threadprocess(void *params){ return 0; } //What i need to do is pass a string from the main() to the threadprocess.. Please Help. //I alsot need to send a string back to the main process //Could be a SOCKET i need to pass back or something for example //but i know i could just put global string but it needs to separate per proccess ran... -- modified at 14:47 Thursday 22nd March, 2007

    M L D S D 5 Replies Last reply
    0
    • D darkcloud 42o

      //hi the type of threading im using is.. #include #include using namespace std; int shutdown=0; int main(){ unsigned sid; (HANDLE)_beginthreadex( NULL, 0, threadprocess, NULL, 0, &sid); while(1){ Sleep(1000); if(shutdown==1){ break; } } } unsigned __stdcall threadprocess(void *params){ return 0; } //What i need to do is pass a string from the main() to the threadprocess.. Please Help. //I alsot need to send a string back to the main process //Could be a SOCKET i need to pass back or something for example //but i know i could just put global string but it needs to separate per proccess ran... -- modified at 14:47 Thursday 22nd March, 2007

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      Whatever you pass as the 4th param to _beginthreadex will be the "params" param in your threadprocess. Mark

      "If you can dodge a wrench, you can dodge a ball."

      D 1 Reply Last reply
      0
      • D darkcloud 42o

        //hi the type of threading im using is.. #include #include using namespace std; int shutdown=0; int main(){ unsigned sid; (HANDLE)_beginthreadex( NULL, 0, threadprocess, NULL, 0, &sid); while(1){ Sleep(1000); if(shutdown==1){ break; } } } unsigned __stdcall threadprocess(void *params){ return 0; } //What i need to do is pass a string from the main() to the threadprocess.. Please Help. //I alsot need to send a string back to the main process //Could be a SOCKET i need to pass back or something for example //but i know i could just put global string but it needs to separate per proccess ran... -- modified at 14:47 Thursday 22nd March, 2007

        L Offline
        L Offline
        led mike
        wrote on last edited by
        #3

        Take a look at the "Threading Articles[^]" here on CodeProject :sigh:

        led mike

        1 Reply Last reply
        0
        • D darkcloud 42o

          //hi the type of threading im using is.. #include #include using namespace std; int shutdown=0; int main(){ unsigned sid; (HANDLE)_beginthreadex( NULL, 0, threadprocess, NULL, 0, &sid); while(1){ Sleep(1000); if(shutdown==1){ break; } } } unsigned __stdcall threadprocess(void *params){ return 0; } //What i need to do is pass a string from the main() to the threadprocess.. Please Help. //I alsot need to send a string back to the main process //Could be a SOCKET i need to pass back or something for example //but i know i could just put global string but it needs to separate per proccess ran... -- modified at 14:47 Thursday 22nd March, 2007

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          darkcloud.42o wrote:

          //What i need to do is pass a string from the main() to the threadprocess.. Please Help.

          void __cdecl threadprocess( void *params )
          {
          char *p = (char *) params;
          strcat(p, "World");
          }

          void main( void )
          {
          char *s = new char[12];
          strcpy(s, "Hello ");
          _beginthread(threadprocess, 0, s);
          ...
          delete [] s;
          s = NULL;
          }


          "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

          "Judge not by the eye but by the heart." - Native American Proverb

          L D 2 Replies Last reply
          0
          • D David Crow

            darkcloud.42o wrote:

            //What i need to do is pass a string from the main() to the threadprocess.. Please Help.

            void __cdecl threadprocess( void *params )
            {
            char *p = (char *) params;
            strcat(p, "World");
            }

            void main( void )
            {
            char *s = new char[12];
            strcpy(s, "Hello ");
            _beginthread(threadprocess, 0, s);
            ...
            delete [] s;
            s = NULL;
            }


            "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

            "Judge not by the eye but by the heart." - Native American Proverb

            L Offline
            L Offline
            led mike
            wrote on last edited by
            #5

            :laugh::laugh::laugh:

            led mike

            1 Reply Last reply
            0
            • M Mark Salsbery

              Whatever you pass as the 4th param to _beginthreadex will be the "params" param in your threadprocess. Mark

              "If you can dodge a wrench, you can dodge a ball."

              D Offline
              D Offline
              darkcloud 42o
              wrote on last edited by
              #6

              error C2664: '_beginthreadex' : cannot convert parameter 4 from 'std::string' to 'void *' If i can get this working i can mod my app quite a bit and do many more functions much more efficiently...

              M 1 Reply Last reply
              0
              • D darkcloud 42o

                error C2664: '_beginthreadex' : cannot convert parameter 4 from 'std::string' to 'void *' If i can get this working i can mod my app quite a bit and do many more functions much more efficiently...

                M Offline
                M Offline
                Mark Salsbery
                wrote on last edited by
                #7

                A 'std::string is not a pointer. The size of the parameter passed is the size of a pointer so that's the largest object you can pass. Maybe passing the address of your 'std::string' will work. You'll have to cast it back to a pointer to a std::string in your threadproc. See DavidCrow's reply. Mark

                "If you can dodge a wrench, you can dodge a ball."

                1 Reply Last reply
                0
                • D David Crow

                  darkcloud.42o wrote:

                  //What i need to do is pass a string from the main() to the threadprocess.. Please Help.

                  void __cdecl threadprocess( void *params )
                  {
                  char *p = (char *) params;
                  strcat(p, "World");
                  }

                  void main( void )
                  {
                  char *s = new char[12];
                  strcpy(s, "Hello ");
                  _beginthread(threadprocess, 0, s);
                  ...
                  delete [] s;
                  s = NULL;
                  }


                  "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

                  "Judge not by the eye but by the heart." - Native American Proverb

                  D Offline
                  D Offline
                  darkcloud 42o
                  wrote on last edited by
                  #8

                  ic that appears to be working but i need to have the char s to be used right after the threadprocess completes... which is modified inside threadprocess... see what i mean..

                  D 1 Reply Last reply
                  0
                  • D darkcloud 42o

                    ic that appears to be working but i need to have the char s to be used right after the threadprocess completes... which is modified inside threadprocess... see what i mean..

                    D Offline
                    D Offline
                    David Crow
                    wrote on last edited by
                    #9

                    darkcloud.42o wrote:

                    ...i need to have the char s to be used right after the threadprocess completes... which is modified inside threadprocess... see what i mean..

                    If you are creating a secondary thread, and then just waiting for it to complete, why bother creating it? In any case, you'll need to implement some sort of synchronization object.


                    "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

                    "Judge not by the eye but by the heart." - Native American Proverb

                    D 1 Reply Last reply
                    0
                    • D David Crow

                      darkcloud.42o wrote:

                      ...i need to have the char s to be used right after the threadprocess completes... which is modified inside threadprocess... see what i mean..

                      If you are creating a secondary thread, and then just waiting for it to complete, why bother creating it? In any case, you'll need to implement some sort of synchronization object.


                      "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

                      "Judge not by the eye but by the heart." - Native American Proverb

                      D Offline
                      D Offline
                      darkcloud 42o
                      wrote on last edited by
                      #10

                      cause i want to have it on a separate thread because it will serv up to 3-10 diff functions each function must be able to operate simultaniously.. eg... it will execute 1-10 threads at once per motherthread and wait for return here... plus i need to use this thread outside the motherthread so its used all over the freeking app.... I have the threads part setup and it will work.. but i have to have the return on that variable.. could be a socket in some other thread some other time... believe me there are networking situations where this is required... i see what your saying i could do it another way with limited function... but i will save days of programing/debugging.. I tell you what you contact me via email and ill send you my app what ive got here... darkcloud.42o@gmail.com -- modified at 17:10 Thursday 22nd March, 2007

                      1 Reply Last reply
                      0
                      • D darkcloud 42o

                        //hi the type of threading im using is.. #include #include using namespace std; int shutdown=0; int main(){ unsigned sid; (HANDLE)_beginthreadex( NULL, 0, threadprocess, NULL, 0, &sid); while(1){ Sleep(1000); if(shutdown==1){ break; } } } unsigned __stdcall threadprocess(void *params){ return 0; } //What i need to do is pass a string from the main() to the threadprocess.. Please Help. //I alsot need to send a string back to the main process //Could be a SOCKET i need to pass back or something for example //but i know i could just put global string but it needs to separate per proccess ran... -- modified at 14:47 Thursday 22nd March, 2007

                        S Offline
                        S Offline
                        Stephen Hewitt
                        wrote on last edited by
                        #11

                        This code leaks the thread HANDLE. From MSDN: "the handle returned by _beginthreadex has to be closed by the caller of _beginthreadex". If you don't need the HANDLE make your code look soemthing like this:

                        HANDLE hThread = (HANDLE)_beginthreadex(NULL, 0, threadprocess, NULL, 0, &sid);
                        CloseHandle(hThread);

                        Note that closing the HANDLE doesn't terminate the thread. You may want to add error checking which I didn't include in my example.

                        Steve

                        D 1 Reply Last reply
                        0
                        • S Stephen Hewitt

                          This code leaks the thread HANDLE. From MSDN: "the handle returned by _beginthreadex has to be closed by the caller of _beginthreadex". If you don't need the HANDLE make your code look soemthing like this:

                          HANDLE hThread = (HANDLE)_beginthreadex(NULL, 0, threadprocess, NULL, 0, &sid);
                          CloseHandle(hThread);

                          Note that closing the HANDLE doesn't terminate the thread. You may want to add error checking which I didn't include in my example.

                          Steve

                          D Offline
                          D Offline
                          darkcloud 42o
                          wrote on last edited by
                          #12

                          Hey if you can convert a handle to socket.. it would work... is it possible...

                          S 1 Reply Last reply
                          0
                          • D darkcloud 42o

                            Hey if you can convert a handle to socket.. it would work... is it possible...

                            S Offline
                            S Offline
                            Stephen Hewitt
                            wrote on last edited by
                            #13

                            I don't understand.

                            Steve

                            1 Reply Last reply
                            0
                            • D darkcloud 42o

                              //hi the type of threading im using is.. #include #include using namespace std; int shutdown=0; int main(){ unsigned sid; (HANDLE)_beginthreadex( NULL, 0, threadprocess, NULL, 0, &sid); while(1){ Sleep(1000); if(shutdown==1){ break; } } } unsigned __stdcall threadprocess(void *params){ return 0; } //What i need to do is pass a string from the main() to the threadprocess.. Please Help. //I alsot need to send a string back to the main process //Could be a SOCKET i need to pass back or something for example //but i know i could just put global string but it needs to separate per proccess ran... -- modified at 14:47 Thursday 22nd March, 2007

                              D Offline
                              D Offline
                              darkcloud 42o
                              wrote on last edited by
                              #14

                              Ok so i got the variable passed to child threads and they can function.. but i need to pass more than 1 variable to the thread char* s and SOCKET ls; lets not worry about the return on the child thread right now.... if i can 2 variables passed the whole process can be done inside child thread.. but one question... about that.. unsigned __stdcall test(void* params){ return 3; ) this return here how can i axs that??? can i access a process like int test(){ return 0; } be accessed from open threads at the same time? btw rigth now its FTP/WEB SERVER with Advanced custom admin functions with GUI. -- modified at 12:40 Friday 23rd March, 2007

                              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