-1

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?

Antiusninja
  • 157
  • 1
  • 17
  • It is probably operating system and implementation and computer specific. And you don't show what your thread are exactly doing and how exactly they are started. Perhaps using posix threads (e.g. on Linux....) and reading some [POSIX thread tutorial](https://computing.llnl.gov/tutorials/pthreads/) could help. – Basile Starynkevitch Jul 19 '15 at 10:41
  • 5
    Unless you set the [*affinity*](https://en.wikipedia.org/wiki/Processor_affinity) the operating system decides which cores your threads will run on, so you could create many more threads and they still may not use all cores. – Some programmer dude Jul 19 '15 at 10:42
  • 2
    because only even numbered cores are busy, I expect to be a 4 core processor with hyper-threading enabled and not a 8-core system – valentin Jul 19 '15 at 10:43
  • I cant post the whole stuff that is done inside DoMatch() because it's huge. One iteration is like that - it creates a 5 byte key with the value of iteration number and puts the ley in a hash function that does lots of asm-level athirmetics to fill a 255 byte table with the full key, then compares with table with the source table. – Antiusninja Jul 19 '15 at 10:55
  • If you won't show code, why do you expect us to comment on it? – David Heffernan Jul 19 '15 at 11:03
  • 4
    @Joachim The scheduler is not stupid. It tries to use all the cpus. – David Heffernan Jul 19 '15 at 11:04
  • So you think there can be something in the body of iterations that prevents CPUs to work at 100% load? Can that be mutexes that WaitForSingleObject to std::cout compare result to the console? I've got one more WaitForSingleObject that checks if all the threads should stop working (bool) – Antiusninja Jul 19 '15 at 11:12
  • 1
    For sure the problem can be in the code you didn't show. Make an mcve. – David Heffernan Jul 19 '15 at 11:32
  • If I make 8 loopy, CPU-intensive threads on my i7 4/8 core box, the CPU use goes to 100%, all 8 bars on the Task Manager 'Performance' tab go green up to the top and the fan goes to max rpm. If you have 8 supposedly CPU-intensive threads and CPU use is at 10-15%, then you have a design/code/synchro issue. – Martin James Jul 19 '15 at 13:53
  • Divide the problem into pieces, disable portions of your `DoMatch()` method until the CPU usage starts to go up, that might help find the troublesome spot. Also, you're not doing any I/O nor allocating memory from the heap at a high frequency? – Chris O Jul 19 '15 at 15:10

1 Answers1

0

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

Antiusninja
  • 157
  • 1
  • 17