C ++ - Questions about multithreading

I'm having trouble understanding some of the concepts of multithreading. I know the basic principles, but I have problems understanding when individual threads are sent and used by the kernels.

I know that having multiple threads allows the code to run in parallel. I think this would be a good addition to my archive extraction program, which could decompress blocks using multiple cores. It decompresses all the files in a for loop, and I hope that every available kernel will work with the file.

Here are my questions:

  • Do I need to query or even take into account the number of cores on a machine, or when threads are running, are they automatically sent to free cores?

  • Can someone show me an example of a for loop using threads. Say, at each iteration of the loop, it will call a function using a different thread. I read that the ideal number of threads that should be active is the number of cores. How to find out when the kernel is free or should I check if it has joined the main thread and create a new thread when it should support a certain number of threads.

Am I deceiving something, or do my questions indicate that I do not understand the concept?

+4
source share
4 answers

, , , , . , 1000 , 1000 , .

, , , , , , . IO , , -, , .

, , ​​ . . , , , , , .

++ 11 .

+3
  • , API, , POSIX Linux (pthread library).

  • , , , API , API- , . , , pthreads ( API- C/++, ).

    #include <stdio.h>
    #include <stdlib.h>        
    #include <pthread.h>
    // Whatever other headers you need for your code.
    
    #define MAX_NUM_THREADS 12
    
    // Each thread will run this function.
    void *worker( void *arg )
    {
        // Do stuff here and it will be 'in parallel'.
    
        // Note: Threads can read from the same location concurrently
        // without issue, but writing to any shared resource that has not been
        // locked with, for example, a mutex, can cause pernicious bugs.
    
        // Call this when you're done.
        pthread_exit( NULL );
    }
    
    int main()
    {
        // Each is a handle for one thread, with 12 in total.
        pthread_t myThreads[MAX_NUM_THREADS];
    
        // Create the worker threads.
        for(unsigned long i = 0; i < numThreads; i++)
        {
            // NULL thread attributes struct.
            // This initializes the threads with the default PTHREAD_CREATE_JOINABLE
            // attribute; we know a thread is finished when it joins, see below.
            pthread_create(&myThreads[i], NULL, worker, (void *)i);
        }
    
        void *status;
        // Wait for the threads to finish.
        for(unsigned int i = 0; i < numThreads; i++)
        {
            pthread_join(myThreads[i], &status);
        }
    
        // That all, folks.
        pthread_exit(NULL);
    }
    

pthreads.

:

, , :

, , . worker decompressFile, pthread_create. . .

, , . , , . , , :

, A A open, Thread B B, A File B . Thread A Thread B, , , , (, ) .

+2

, , / parallelism, /, , , parallelism , ..

, , . ++, , . Intel TBB, Microsoft PPL, AMD Bolt, Quallcomm MARE, . , , , .

:

1) , / . , . , , (, - -), , . .

2) for, tbb::parallel_for ++ 11 -:

#include <tbb/tbb.h>
void ParallelFoo( std::vector<MyDataType>& v ) {
    tbb::parallel_for( size_t(0), v.size(), [&](int i){
        Foo( v[i] );
    } );
}

, , ; ; , , - , .

: Intel TBB.

+1
source

If you are on Windows, you can take a look at thread pools, a good description can be found here: http://msdn.microsoft.com/en-us/magazine/cc163327.aspx . An interesting feature of this tool is that it promises to manage threads for you. He also chooses the optimal number of threads depending on demand, as well as on the available cores.

0
source

All Articles