-
August 16th, 2016, 02:57 AM
#1
Concurrency vs parallelism in c++
Hello,
I just started using parallel programming and I have a couple of questions.
1.Can some one give me an example in c++ of thread executing congruently and threads executing in parallel. When I say congruent and parallel I mean the following:
Concurrency is essentially when two tasks are being performed at the same time. This might mean that one is 'paused' for a short duration, while the other is being worked on
Parallelism requires that at least two processes/tasks are actively being performed at a particular moment in time.
If I use join() (#include <thread>) is it parallel or concurrent.
2. If I have 4 cores how can I calculate the optimal number of threads that I have to create so that I have the best performance? Performance is really important in my case.
3. I'm using Visual Studio Community 2015 on Windows 4 core machine and I noticed something weird when I put a break point before the a start joining the threads and I go through the rest of the code it takes a lot less time that If I put a break point after the last join and I go through the rest of the code. WHY ?
Code:
void task(int i){
//do something
}
int main{
//do smt
std::thread t1(task(1));
// and so on I create 15 threads
t1.join();
// and so on
// do something else
}
5. And my last question if I have a for loop that take 16 seconds and each iteration is independent and I want to do it in parallel. I have 4 cores. What is the best time I can get after parallelising, 4 seconds + a couple of ms for creating the threads ???
-
August 16th, 2016, 04:05 AM
#2
Re: Concurrency vs parallelism in c++
1. No. Within c++, threads are created. How they execute is down to the OS. In your terminology, if there are available 'free' cores then threads will execute in parallel across the cores otherwise if there are no available 'free' cores then at least some threads will execute congruently. Normally this is outside of your control. Without getting down and dirty with the specifics of various Windows API functions, there are ways of affiliating threads to cores etc etc but this is not part of c++ but part of the underlying OS functions. See https://msdn.microsoft.com/en-us/lib...(v=vs.85).aspx for Windows details. If you're just using c++, then when you create threads how and when they execute is down to the OS (except pause, resume etc).
2. If your threads are all CPU-bound (?) then the maximum number of threads is the number of cores. However there is one caveat on this. Some processors allow hyper-threading which doubles the number of available cores by creating virtual cores (see https://en.wikipedia.org/wiki/Hyper-threading). If your code is totally CPU-bound then using hyper-threading might degrade performance.
5. Try it and see! This depends upon factors which you haven't described - the number of iterations, what the loop is undertaking, how you propose to parallel etc.
Last edited by 2kaud; August 16th, 2016 at 06:49 AM.
Reason: Change 4. to 5. What happened to 4.?
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
August 16th, 2016, 05:44 AM
#3
Re: Concurrency vs parallelism in c++
This article may be of interest. It's fairly old so doesn't use c++ threads but it's points are still valid.
https://msdn.microsoft.com/en-us/library/ms810437.aspx
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
August 16th, 2016, 06:19 AM
#4
Re: Concurrency vs parallelism in c++
1. You need to understand std::thread::join. Join does nothing but waiting for thread closure. Blunt waiting, is it parallelism or concurrency?
3. You're trying to draw conclusions (and possibly put up some theory on that) basing on VC debugger behavior. Please don't.
5. Let's say your independent tasks are equivalent in length. You're going to have 4 seconds + some overhead which amount you're never able to predict down to milliseconds. Non-real time OS always to expose the overhead for your particular process. Just because your process is willingly given a CPU time (user mode as well as kernel mode), under OS supervision and depending on OS resources availability, but not exclusively hijacks the hardware.
Best regards,
Igor
-
August 17th, 2016, 01:51 PM
#5
Re: Concurrency vs parallelism in c++
All counsel is offered in compliance with common decency as it were. You are at last in charge of impacts of your projects and the trustworthiness of the machines they keep running on.
-
August 17th, 2016, 02:12 PM
#6
Re: Concurrency vs parallelism in c++
Originally Posted by NathanaelTillman
All counsel is offered in compliance with common decency as it were. You are at last in charge of impacts of your projects and the trustworthiness of the machines they keep running on.
and this is helpful for the OP how?
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|