window multithreading

i am trying to encrypt text using multi thread, when i encrypt large text which is 4 MB file using one thread, the time taken to encrypt is 10 seconds. but when i use two threads, the time taken to encrypt is 25 seconds. i have no idea why the time taken is longer when using two threads. time is supposed to be shorter than one thread encryption.

in my program i use encrypt function to perform encryption, first, get the string from text file and store in string value and divide half and store into vector. in two threads, first thread encrypts first part of string and second thread encrypts the other second part.
I'm running on intel i5 processor and 4gb memory. how to allocate the thread to each processor in order to encrypt faster.

can anyone help me solve the problem... thanks..

below is my code:

string Enc(string pt)
{
byte key[ CryptoPP::AES::DEFAULT_KEYLENGTH ], iv[ CryptoPP::AES::BLOCKSIZE ];

string cip;
CryptoPP::CTR_Mode<CryptoPP::AES>::Encr… Encryptor( key, sizeof(key), iv );
CryptoPP::StringSource( pt, true,new CryptoPP::StreamTransformationFilter
( Encryptor,new CryptoPP::HexEncoder(new CryptoPP::StringSink(cip))));
return cip;
}

int main()
{

HANDLE hThreads[10];

if(NUM==1)
hThreads[0] = (HANDLE)_beginthread(Func1, 0, NULL);

if(NUM==2)
{
hThreads[0] = (HANDLE)_beginthread(Func1, 0, NULL);
hThreads[1] = (HANDLE)_beginthread(Func2, 0, NULL);
}
WaitForMultipleObjects(NUM, hThreads, TRUE, INFINITE);
}

void Func1(void *P)
{

Enc(vec.at(0));
return;
}

void Func2(void *P)
{
Enc(vec2.at(0));
return;
}
There are a number of issues here:

1. Please use code tags. Read http://cplusplus.com/articles/z13hAqkS/ for details.
2. _beginthread() is not very good. Use _beginthreadex() instead for two main reasons: If the new thread exits too quickly, the handle returned by _beginthread() is invalid, and most importantly, only the handles returned by _beginthreadex() can be used for synchronization (as in WaitForMultipleObjects()). See http://msdn.microsoft.com/en-us/library/kdzttdcb(VS.80).aspx for details. In the remarks section, you'll read:

MSDN Online wrote:
You are also able to use the thread handle returned by _beginthreadex with the synchronization APIs, which you cannot do with _beginthread.


3. Your function Enc() accepts a string. Are you then saying that the variable vec is of type std::vector<std::string>? Why do you need a vector, much less 2 vectors??

4. In order to split the string contents in a text file in two, you only need two std::string objects. I don't see the need for a vector here.
Topic archived. No new replies allowed.