Multithread problem

L

lumo2000

hello NG,
i wrote a single threaded program and want to multithread it now.
what is it doing?
it calls another program and waits for it to finish. (passing some
parameters to the other program)
so i am cycling through a textfile grabbing the parameters for the
program to call (working in singlethread)

so i tried to "lock" the access to the list...
i lock access to the list give one thread the command line he needs
and then unlock again.
i am doing this in while loops.
somehow c++ is too fast to work this way. so how can i solve this?

i am using _beginthread() and _endthread
http://msdn2.microsoft.com/en-us/library/kdzttdcb(VS.71).aspx

here is my current code:

// popularNameGrabber.cpp : Definiert den Einstiegspunkt für die
Konsolenanwendung.
//

#include "stdafx.h"
#include "time.h" /* clock for wait function */
#include <process.h> /* _beginthread, _endthread */

//#include "fstream.h"

bool listexists();
void getPopularNames();

int counter = 0;
bool b_locked = false;
int childThreadCount = 0;
std::string call;

int _tmain(int argc, _TCHAR* argv[])
{
if (listexists())
printf("taxa list file found!\n");
else
printf("error 404 - file [%S] not found\n");
getPopularNames();
// to get popular name call php file with latin name as parameter
// php.exe getPopularName.php "latin name"
return 0;
}

void wait(unsigned int mseconds)
{
clock_t goal = mseconds + clock();
while (goal > clock());
}

void increaseCounter()
{
counter++;
}

void decreaseChildThreadCount()
{
childThreadCount--;
}

void increaseChildThreadCount()
{
childThreadCount++;
}

bool unlock()
{
b_locked = false;
return b_locked;
}

bool lock()
{
b_locked = true;
return b_locked;
}

bool locked()
{
return b_locked;
}

const char* getCommand()
{
return call.c_str();
}

void getterThread( void* dummy /*const char* Command*/)
{
cout << ".";
while (locked()) {wait(50);}
lock();
const char* command = getCommand();
unlock();
system(command);
counter++;
decreaseChildThreadCount;

_endthread(); // close the thread
}



void getPopularNames()
{
ifstream filestr;
std::string name;
//char* name;
std::string res;
filestr.open("taxa.liste",ios::in);
cout << "processing";
if( filestr.is_open() )
{
while( getline(filestr, name) ) {
//cout << name << '\n';

while (childThreadCount > 20 || locked())
{
//wait xxx ms...
wait(50);
}

call = "php.exe getPopularName.php \"" + name +"\"";

increaseChildThreadCount();
_beginthread(getterThread, 0, NULL);
}
}
while (childThreadCount!=0)
{
wait(50);
}
cout << "done!\nprocessed " << counter << " latin names.";
filestr.close();
}

bool listexists()
{
bool flag = false;
fstream filestr;
filestr.open("taxa.liste",ios::in);
if( filestr.is_open() )
{
flag=true;
}
filestr.close();
return flag;
}
 
J

Jim Langston

hello NG,
i wrote a single threaded program and want to multithread it now.
what is it doing?
it calls another program and waits for it to finish. (passing some
parameters to the other program)
so i am cycling through a textfile grabbing the parameters for the
program to call (working in singlethread)

so i tried to "lock" the access to the list...
i lock access to the list give one thread the command line he needs
and then unlock again.
i am doing this in while loops.
somehow c++ is too fast to work this way. so how can i solve this?

i am using _beginthread() and _endthread
http://msdn2.microsoft.com/en-us/library/kdzttdcb(VS.71).aspx

here is my current code:

[Snip code]

Try comp.programming.threads
 
T

turbovince

hello NG,
i wrote a single threaded program and want to multithread it now.
what is it doing?
it calls another program and waits for it to finish. (passing some
parameters to the other program)
so i am cycling through a textfile grabbing the parameters for the
program to call (working in singlethread)

so i tried to "lock" the access to the list...
i lock access to the list give one thread the command line he needs
and then unlock again.
i am doing this in while loops.
somehow c++ is too fast to work this way. so how can i solve this?

i am using _beginthread() and _endthreadhttp://msdn2.microsoft.com/en-us/library/kdzttdcb(VS.71).aspx

here is my current code:

[Snip code]

Try comp.programming.threads

Google "reader writer problem", what you're looking for is probably
mutexes/semaphores.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,149
Members
46,695
Latest member
StanleyDri

Latest Threads

Top