Reputation: 167
I'm learning to use multithreading on a simple keygen example. I've implemented hashkeys matching algorhythm that increments one of the keys and compares it to the original and it should stop once two hashkeys match. So I need to iterate over 0xffffffffff different hashkeys to find the match. I've read some msdn on the topic and decided to start 8 threads of the same matching for loop starting with thread_number_offset and incrementing each iteration by number_of_threads. Here is the starter code:
struct Args{
Args(char* in_CT, Chunk* in_chunk, int in_thread_id, int in_stride) :
stride{in_stride},
input_CT{ in_CT },
chunk{ in_chunk },
thread_id{ in_thread_id }{}
Chunk* chunk;
char* input_CT;
int thread_id;
int stride;
};
void Match(void *args){
Args *a = (Args*)args;
a->chunk->MatchCTMP(a->input_CT,a->thread_id,a->stride);
}
for (unsigned int i = 0; i < threads_num; ++i){
CTTable[i] = new char[0x100];
ArgsTable[i] = new Args(CTTable[i], &in_chunk, i, threads_num);
_beginthread(ThreadFunc, 0, ArgsTable[i]);
}
here is the function with the for loop:
void Chunk::MatchCTMP(char* in_dest_CT,int in_thread_id, int in_stride){
unsigned int i = in_thread_id;
unsigned int i_end = MAX - i;
for (; i < i_end; i += in_stride){
hash[0] = i & 0xff;
...
...HashFunction(in_dest_CT);//CT is Compare Table
...CompareFunction(in_dest_CT, in_source_CT);
}
}
Everything is fine, but the CPU load in task manager window doesnt increase above 10-15 percent; Also not all cores are actually loaded. Cores 0,2,4,6 are busy while 1,3,5,7. I thought that constant iteration on maximum number of cpu cores should immediately load the CPU at it's full capability. Why is that happening? Is it much more complicated than what I did?
Upvotes: -1
Views: 193
Reputation: 167
Ok, I've got this kind of construct after disabling which it I've got 100% CPU usage:
WaitForSingleObject(mutex, INFINITE);
std::cout << buffer;
ReleaseMutex(mutex);
where buffer was a char buffer[256] So that was some kind of synchronization issue that I'm going to dig next
Upvotes: 0