Passing Variables to Thread Process [modified]
-
//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
-
//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
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
-
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
-
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."
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...
-
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...
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."
-
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
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..
-
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..
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
-
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
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
-
//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
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 theHANDLE
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
-
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 theHANDLE
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
Hey if you can convert a handle to socket.. it would work... is it possible...
-
Hey if you can convert a handle to socket.. it would work... is it possible...
I don't understand.
Steve
-
//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
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