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;
}
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;
}