Ami, I wrote this very quickly to illustrate the bucket/worker threads
method. Attached is:
TestMaster.Cpp
TestSlave.cpp
Note: I wrote this very quickly, and its proof of concept. I'm sure
there are part that can be done more cleanly or C++ correct.
Attached is slightly cleaner testmaster, CBucket had missing
destructor, added sync to Add function.
Note, I change the constant MAX_WORKERS = 20 and it screamed on my
machine. :-) With good simulation of testslave, you can make your
master pretty solid.
[testmaster.cpp4K ]#include <stdio.h>
#include <afx.h>
#include <afxtempl.h>
#include <conio.h>
//------------------------------------------------------
const DWORD MAX_JOBS = 100;
const DWORD MAX_WORKERS = 20;
const char *SLAVE_EXE = "testslave.exe";
typedef struct _tagTSlaveData {
char szUser[256];
char szPwd[256];
char szHost[256];
} TSlaveData;
typedef struct _tagTThreadData {
DWORD index;
DWORD dwStartTime;
DWORD dwEndTime;
DWORD dwExitCode;
TSlaveData sd;
} TThreadData;
class CBucket : public CList< TSlaveData, TSlaveData>
{
public:
CBucket() { InitializeCriticalSection(&cs); }
~CBucket() { DeleteCriticalSection(&cs); }
void Add( const TSlaveData &o )
{
EnterCriticalSection(&cs);
AddHead( o );
LeaveCriticalSection(&cs);
}
void Add(const char *user, const char *pwd, const char *host)
{
TSlaveData td = {0};
strncpy(td.szUser,user,sizeof(td.szUser));
strncpy(td.szPwd,pwd,sizeof(td.szPwd));
strncpy(td.szHost,host,sizeof(td.szHost));
Add(td);
}
BOOL Fetch(TSlaveData &o)
{
EnterCriticalSection(&cs);
BOOL res = !IsEmpty();
if (res) o = RemoveTail();
LeaveCriticalSection(&cs);
return res;
}
private:
CRITICAL_SECTION cs;
} Bucket;
//----------------------------------------------------------------
// Slave Work
//----------------------------------------------------------------
BOOL SlaveWork(TThreadData *data)
{
STARTUPINFO si;
ZeroMemory(&si, sizeof(si));
PROCESS_INFORMATION pi;
ZeroMemory(&pi, sizeof(pi));
CString sCmd;
sCmd.Format("%s %d /user:%s /pwd:%s /host:%s",
SLAVE_EXE,
data->index+1,
data->sd.szUser,
data->sd.szPwd,
data->sd.szHost);
if (!CreateProcess(NULL, (LPSTR &)sCmd,
NULL, NULL, FALSE, 0, NULL=
Many thanks for such a detailed explanation. your code rocks. I
problem.
Thanks a bunch again.. you really ROCK!!