Using threads C++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 23

Using threads C++

I'm trying to do something very simple, I have two threads, one only prints a char in a for loop and other thread gets a char. The idea behind this is running the for loop thread and asking at the same time a char from the user, if that char is equal to 's', stops the for loop. The code is divided in two, one where I only use two std::threads, using that approach I can press a key from the keyboard while the for loop thread is running at the same time and get that char, but I don't know how to get that char value from the keyboard to use it in my for loop thread and stop it (35-57). Reading a little bit I found that I can get a return value from a thread using std::shared_future, I tried that approach (7-32) but the the problem is, the thread for loop stops to get the user's char. I'm new using threads and not sure why both approaches aren't working. Here's the code: https://code.sololearn.com/cxk8FMPNJJr9

3rd Dec 2020, 9:42 AM
Eduardo Perez Regin
Eduardo Perez Regin - avatar
8 Answers
+ 20
[4 of 5] Talking about return value ~The value v stored in the shared state, as std::move(v)... ~The reference stored as value in the shared state! If an exception was stored in the shared state referenced by the future (e.g. via a call to std::promise::set_exception()) then that exception will be thrown! The thing you should note is - The implementations are encouraged to detect the case when valid() is false before the call and throw a std::future_error with an error condition of std::future_errc::no_state.
5th Dec 2020, 12:28 PM
Piyush
Piyush - avatar
+ 17
[5 of 5] State - https://code.sololearn.com/c3J3xbix3ug5/?ref=app At last I wanna say that Futures need to be used together with promises. If you don't have anything providing data into the promise that is attached to the future, the future will never be fulfilled. Info regarding last paragraph ~https://www.modernescpp.com/index.php/promise-and-future Hope helps you👍
5th Dec 2020, 12:29 PM
Piyush
Piyush - avatar
+ 16
[3 of 5] The get method waits until the future has a valid result and (depending on which template is used) retrieves it. It effectively calls wait() in order to wait for the result. The generic template and two template specializations each contain a single version of get. The three versions of get differ only in the return type. The behavior is undefined if valid() is false before the call to this function. Any shared state is released. valid() is false after a call to this method. P.s - There are no parameters in it!
5th Dec 2020, 12:27 PM
Piyush
Piyush - avatar
+ 15
[1 of 5] Eduardo Perez Regin hash! Finally am done with my answer! --- char stop = f.get(); // IT STOPS. you can use this! Though am having a question that do you just want to get keyboard input or what? im asking because depending on what you do, windows (or whatever system you use) or std might already have that what you need except of course if you want to try it yourself or need to to do it yourself!
5th Dec 2020, 12:25 PM
Piyush
Piyush - avatar
+ 15
[2 of 5] I will try to help as much as I can! I do like to elaborate std::future<T>::get in order to help you in the query! ~T get(); (1) (member only of generic future template) (since C++11) T& get(); (2) (member only of future<T&> template specialization) (since C++11) void get(); (3) (member only of The get method waits until the future has a valid result and (depending on which template is used) retrieves it. It effectively calls wait() in order to wait for the result. future<void> template specialization) (since C++11)
5th Dec 2020, 12:26 PM
Piyush
Piyush - avatar
+ 11
Eduardo Perez Regin always welcome 🍫
15th Dec 2020, 1:03 PM
Piyush
Piyush - avatar
+ 1
Piyush[21 Dec❤️] thank you so much for you answer! Honestly, I solved it days ago (but because homework I forgot I did this question 😅). The way I did it was using global variables , a source that is shared between threads. Of course doing this you need to be careful about deadlocks, race condition and more, but it was my solution at that moment . Thank you so much for you answer!!
10th Dec 2020, 8:31 AM
Eduardo Perez Regin
Eduardo Perez Regin - avatar
- 2
fgg
6th Dec 2020, 12:23 AM
ali
ali - avatar