Run functions in parallel c++
I have a class A
, it has 4 methods. I'm creating two instances a,b
. What I need is to run both a.function 1 to 4
and b.function 1 to 4
at the same time in c++?
c++
add a comment |
I have a class A
, it has 4 methods. I'm creating two instances a,b
. What I need is to run both a.function 1 to 4
and b.function 1 to 4
at the same time in c++?
c++
1
You need to use threads.
– paddy
Nov 14 '18 at 4:59
Threads, perhaps?ta = std::thread(&A::function, &a); tb = std::thread(&A::function, &b); ta.join(); tb.join();
– Severin Pappadeux
Nov 14 '18 at 5:00
Have you tried before asking this question?
– Ved Prakash
Nov 14 '18 at 5:04
Pay attention that the situation becomes very difficult if you have a data race, for example if both threads try to modify the same variable.
– Damien
Nov 14 '18 at 9:30
add a comment |
I have a class A
, it has 4 methods. I'm creating two instances a,b
. What I need is to run both a.function 1 to 4
and b.function 1 to 4
at the same time in c++?
c++
I have a class A
, it has 4 methods. I'm creating two instances a,b
. What I need is to run both a.function 1 to 4
and b.function 1 to 4
at the same time in c++?
c++
c++
edited Nov 14 '18 at 6:27
Shrikanth N
510211
510211
asked Nov 14 '18 at 4:57
Ragavan KalatharanRagavan Kalatharan
4
4
1
You need to use threads.
– paddy
Nov 14 '18 at 4:59
Threads, perhaps?ta = std::thread(&A::function, &a); tb = std::thread(&A::function, &b); ta.join(); tb.join();
– Severin Pappadeux
Nov 14 '18 at 5:00
Have you tried before asking this question?
– Ved Prakash
Nov 14 '18 at 5:04
Pay attention that the situation becomes very difficult if you have a data race, for example if both threads try to modify the same variable.
– Damien
Nov 14 '18 at 9:30
add a comment |
1
You need to use threads.
– paddy
Nov 14 '18 at 4:59
Threads, perhaps?ta = std::thread(&A::function, &a); tb = std::thread(&A::function, &b); ta.join(); tb.join();
– Severin Pappadeux
Nov 14 '18 at 5:00
Have you tried before asking this question?
– Ved Prakash
Nov 14 '18 at 5:04
Pay attention that the situation becomes very difficult if you have a data race, for example if both threads try to modify the same variable.
– Damien
Nov 14 '18 at 9:30
1
1
You need to use threads.
– paddy
Nov 14 '18 at 4:59
You need to use threads.
– paddy
Nov 14 '18 at 4:59
Threads, perhaps?
ta = std::thread(&A::function, &a); tb = std::thread(&A::function, &b); ta.join(); tb.join();
– Severin Pappadeux
Nov 14 '18 at 5:00
Threads, perhaps?
ta = std::thread(&A::function, &a); tb = std::thread(&A::function, &b); ta.join(); tb.join();
– Severin Pappadeux
Nov 14 '18 at 5:00
Have you tried before asking this question?
– Ved Prakash
Nov 14 '18 at 5:04
Have you tried before asking this question?
– Ved Prakash
Nov 14 '18 at 5:04
Pay attention that the situation becomes very difficult if you have a data race, for example if both threads try to modify the same variable.
– Damien
Nov 14 '18 at 9:30
Pay attention that the situation becomes very difficult if you have a data race, for example if both threads try to modify the same variable.
– Damien
Nov 14 '18 at 9:30
add a comment |
1 Answer
1
active
oldest
votes
It seem to me, what you are asking for is multi-threading. For C++ you may rely on native thread support or libraries like boost,which is cross platform and is widely used in industry. High level languages like Java and C# have the functionality for multi threading in built ,which is much easier to do this.
You can implement this is with the future library (#include <future>
).
If your functions are
int my_func(int param1, int param2, int param3);
int my_second_func(int param1, int param2, int param3);
Then you can use future to run the functions asynchronously
as follows:
std::future<int> f = std::async(std::launch::async, my_second_func, arg1, arg2, arg3);
int result1 = my_func(arg1, arg2, arg3);
int result2 = f.get();
Where the functions are evaluated in separate threads, asynchronously (in parallel).
2
C++ has native thread support also.
– bolov
Nov 14 '18 at 5:28
1
I see . Thank You .
– Md. Mokammal Hossen Farnan
Nov 14 '18 at 5:50
1
C+11 and above has lot of capabilities in-built that were earlier provided by boost libraries. You can check out the link: thispointer.com/…
– Shrikanth N
Nov 14 '18 at 6:01
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53293417%2frun-functions-in-parallel-c%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
It seem to me, what you are asking for is multi-threading. For C++ you may rely on native thread support or libraries like boost,which is cross platform and is widely used in industry. High level languages like Java and C# have the functionality for multi threading in built ,which is much easier to do this.
You can implement this is with the future library (#include <future>
).
If your functions are
int my_func(int param1, int param2, int param3);
int my_second_func(int param1, int param2, int param3);
Then you can use future to run the functions asynchronously
as follows:
std::future<int> f = std::async(std::launch::async, my_second_func, arg1, arg2, arg3);
int result1 = my_func(arg1, arg2, arg3);
int result2 = f.get();
Where the functions are evaluated in separate threads, asynchronously (in parallel).
2
C++ has native thread support also.
– bolov
Nov 14 '18 at 5:28
1
I see . Thank You .
– Md. Mokammal Hossen Farnan
Nov 14 '18 at 5:50
1
C+11 and above has lot of capabilities in-built that were earlier provided by boost libraries. You can check out the link: thispointer.com/…
– Shrikanth N
Nov 14 '18 at 6:01
add a comment |
It seem to me, what you are asking for is multi-threading. For C++ you may rely on native thread support or libraries like boost,which is cross platform and is widely used in industry. High level languages like Java and C# have the functionality for multi threading in built ,which is much easier to do this.
You can implement this is with the future library (#include <future>
).
If your functions are
int my_func(int param1, int param2, int param3);
int my_second_func(int param1, int param2, int param3);
Then you can use future to run the functions asynchronously
as follows:
std::future<int> f = std::async(std::launch::async, my_second_func, arg1, arg2, arg3);
int result1 = my_func(arg1, arg2, arg3);
int result2 = f.get();
Where the functions are evaluated in separate threads, asynchronously (in parallel).
2
C++ has native thread support also.
– bolov
Nov 14 '18 at 5:28
1
I see . Thank You .
– Md. Mokammal Hossen Farnan
Nov 14 '18 at 5:50
1
C+11 and above has lot of capabilities in-built that were earlier provided by boost libraries. You can check out the link: thispointer.com/…
– Shrikanth N
Nov 14 '18 at 6:01
add a comment |
It seem to me, what you are asking for is multi-threading. For C++ you may rely on native thread support or libraries like boost,which is cross platform and is widely used in industry. High level languages like Java and C# have the functionality for multi threading in built ,which is much easier to do this.
You can implement this is with the future library (#include <future>
).
If your functions are
int my_func(int param1, int param2, int param3);
int my_second_func(int param1, int param2, int param3);
Then you can use future to run the functions asynchronously
as follows:
std::future<int> f = std::async(std::launch::async, my_second_func, arg1, arg2, arg3);
int result1 = my_func(arg1, arg2, arg3);
int result2 = f.get();
Where the functions are evaluated in separate threads, asynchronously (in parallel).
It seem to me, what you are asking for is multi-threading. For C++ you may rely on native thread support or libraries like boost,which is cross platform and is widely used in industry. High level languages like Java and C# have the functionality for multi threading in built ,which is much easier to do this.
You can implement this is with the future library (#include <future>
).
If your functions are
int my_func(int param1, int param2, int param3);
int my_second_func(int param1, int param2, int param3);
Then you can use future to run the functions asynchronously
as follows:
std::future<int> f = std::async(std::launch::async, my_second_func, arg1, arg2, arg3);
int result1 = my_func(arg1, arg2, arg3);
int result2 = f.get();
Where the functions are evaluated in separate threads, asynchronously (in parallel).
edited Nov 14 '18 at 8:05
piet.t
10k63245
10k63245
answered Nov 14 '18 at 5:16
Md. Mokammal Hossen FarnanMd. Mokammal Hossen Farnan
587320
587320
2
C++ has native thread support also.
– bolov
Nov 14 '18 at 5:28
1
I see . Thank You .
– Md. Mokammal Hossen Farnan
Nov 14 '18 at 5:50
1
C+11 and above has lot of capabilities in-built that were earlier provided by boost libraries. You can check out the link: thispointer.com/…
– Shrikanth N
Nov 14 '18 at 6:01
add a comment |
2
C++ has native thread support also.
– bolov
Nov 14 '18 at 5:28
1
I see . Thank You .
– Md. Mokammal Hossen Farnan
Nov 14 '18 at 5:50
1
C+11 and above has lot of capabilities in-built that were earlier provided by boost libraries. You can check out the link: thispointer.com/…
– Shrikanth N
Nov 14 '18 at 6:01
2
2
C++ has native thread support also.
– bolov
Nov 14 '18 at 5:28
C++ has native thread support also.
– bolov
Nov 14 '18 at 5:28
1
1
I see . Thank You .
– Md. Mokammal Hossen Farnan
Nov 14 '18 at 5:50
I see . Thank You .
– Md. Mokammal Hossen Farnan
Nov 14 '18 at 5:50
1
1
C+11 and above has lot of capabilities in-built that were earlier provided by boost libraries. You can check out the link: thispointer.com/…
– Shrikanth N
Nov 14 '18 at 6:01
C+11 and above has lot of capabilities in-built that were earlier provided by boost libraries. You can check out the link: thispointer.com/…
– Shrikanth N
Nov 14 '18 at 6:01
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53293417%2frun-functions-in-parallel-c%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
You need to use threads.
– paddy
Nov 14 '18 at 4:59
Threads, perhaps?
ta = std::thread(&A::function, &a); tb = std::thread(&A::function, &b); ta.join(); tb.join();
– Severin Pappadeux
Nov 14 '18 at 5:00
Have you tried before asking this question?
– Ved Prakash
Nov 14 '18 at 5:04
Pay attention that the situation becomes very difficult if you have a data race, for example if both threads try to modify the same variable.
– Damien
Nov 14 '18 at 9:30