how to write a correct Multi Threaded Application [modified]
-
hi guys, i have a dialog application, i want to launch 10 worker thread and each worker thread will do a particular work, ie. each worker thread will connect to a url and download the page content, and they will parse the page and write some data to a database, so imagine if there are 1000 pages then if i go by general way of geting pages one by one it will take me around 20 mins to 45 mins, so i was thinking if launch 10 thread which in term each will collect 1 page then the time may be reduced, my problem or query is, as fas of today is, we all using single processor, so no matter how many threads u create, any given point of time only one instruction will carried out, but when i launch more then one application of mine and break up the collection say app1 collects 100 and app2 collects another 100, this is faster then collecting 1000 in 1 application, so breakin it to threads will help or not, how to code for it, do you have any idea i mean any design for it, or any help for it, cas i cant write 10 diff function for each thread,i want one function to do all the work but i will pass diff parameter for each thread, but then comes the problem resource access violation if i call the same function for each thread, so is there a way i can the thread callback to a class, or i can put the whole code, for say class mythrdcls { mythrdcls(char *) {} void create(){} void dothetask(){}// like a thread callback function void running(){} void destroy(){} } so please give me an idea how to do it, ya one more thing i forgot to add here, when i call createthread or afxbeginthread, i have to call a thread callback, i want to pass some arguments to my thread callback funcion, so i can pass them in param value, i want to pass more then one value so i have to create a structure and pass it, but the problem is in thread function it does not reflect the correct value, ie struct th { int x;int y} x; x.y=10,x.x=1 beginthread(function1, x); x.y=20,x.x=2 beginthread(function2, x); function1() a=x; a.x///////////does not alwys contain 1 ie cas by the time callback is called the value is changed to 2 so how can make sure tht unless the thread callback is called the 2nd thread cant be created or ast thread get the corect value and so 2nd thread also, i used waitforsingleobject but it did not help Thanks a lot
-
hi guys, i have a dialog application, i want to launch 10 worker thread and each worker thread will do a particular work, ie. each worker thread will connect to a url and download the page content, and they will parse the page and write some data to a database, so imagine if there are 1000 pages then if i go by general way of geting pages one by one it will take me around 20 mins to 45 mins, so i was thinking if launch 10 thread which in term each will collect 1 page then the time may be reduced, my problem or query is, as fas of today is, we all using single processor, so no matter how many threads u create, any given point of time only one instruction will carried out, but when i launch more then one application of mine and break up the collection say app1 collects 100 and app2 collects another 100, this is faster then collecting 1000 in 1 application, so breakin it to threads will help or not, how to code for it, do you have any idea i mean any design for it, or any help for it, cas i cant write 10 diff function for each thread,i want one function to do all the work but i will pass diff parameter for each thread, but then comes the problem resource access violation if i call the same function for each thread, so is there a way i can the thread callback to a class, or i can put the whole code, for say class mythrdcls { mythrdcls(char *) {} void create(){} void dothetask(){}// like a thread callback function void running(){} void destroy(){} } so please give me an idea how to do it, ya one more thing i forgot to add here, when i call createthread or afxbeginthread, i have to call a thread callback, i want to pass some arguments to my thread callback funcion, so i can pass them in param value, i want to pass more then one value so i have to create a structure and pass it, but the problem is in thread function it does not reflect the correct value, ie struct th { int x;int y} x; x.y=10,x.x=1 beginthread(function1, x); x.y=20,x.x=2 beginthread(function2, x); function1() a=x; a.x///////////does not alwys contain 1 ie cas by the time callback is called the value is changed to 2 so how can make sure tht unless the thread callback is called the 2nd thread cant be created or ast thread get the corect value and so 2nd thread also, i used waitforsingleobject but it did not help Thanks a lot