none
Parallel programming using threads

    Question

  • Hello,

    I'm new to parallelism and I'm having some trouble understanding how it works.

    I'm working on a Windows 7 64-bit 4 core machine. 

    This is what I have tried so far.

    #include <thread>

    void t1(){//do something} void t2(){//do something} void t3(){//do something} void t4(){//do something} int main(){ std::thread thread1(t1); std::thread thread2(t2); std::thread thread3(t3); std::thread thread4(t4);

    t1.join();

    t2.join();

    t3.join();

    t4.join();}

    1. How can I calculate the optimal number of thread I should use in order to minimise the execution time of the program?(is there a formula depending on the number of cores and the execution time of 1 thread)

    2. I also don't really understand the joint() method.  I know that it blocks the thread until it has finished the task. But in the code I wrote above do t1,t2,t3,t4 execute at the same time ? So if give them the sam workload they will be done at the same time(+ a couple of milliseconds  for t2,t3,t4) Is it faster if I give more work to first and a bit less to each following thread or the inverse less work to the to the first and a bit more to the second ? Or should I always try to give the same workload to each one of them?

    3. Is there a way I can tell each thread on which core it should execute? Is it a good idea or should I leave the OS Scheduler to do that ? My purpose is execute my program as fast as possible.

    Thanks 



    • Edited by sadasir Monday, August 15, 2016 6:35 PM
    • Moved by litdev Tuesday, August 16, 2016 6:20 PM
    Monday, August 15, 2016 4:33 PM

Answers

  • Hi !

    i think your programing in C++

    if it is

    i would recommend to post your question on the : Visual C++ in Visual Studio 2015 forum

    link:  https://msdn.microsoft.com/en-us/library/60k1461a.aspx

    • Marked as answer by litdev Tuesday, August 16, 2016 6:19 PM
    Tuesday, August 16, 2016 2:09 AM

All replies

  • Hi !

    i think your programing in C++

    if it is

    i would recommend to post your question on the : Visual C++ in Visual Studio 2015 forum

    link:  https://msdn.microsoft.com/en-us/library/60k1461a.aspx

    • Marked as answer by litdev Tuesday, August 16, 2016 6:19 PM
    Tuesday, August 16, 2016 2:09 AM
  • C++ forum can be found here.

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=vcgeneral

     

     

     



    Regards, Dave Patrick ....
    Microsoft Certified Professional
    Microsoft MVP [Windows Server] Datacenter Management

    Disclaimer: This posting is provided "AS IS" with no warranties or guarantees, and confers no rights.

    • Proposed as answer by Mike Laughlin Wednesday, August 17, 2016 2:11 PM
    Wednesday, August 17, 2016 1:23 PM
    Moderator