Looping back again to Start - visual-c++

now I'm Having problem in repeating the loop after it finished doing the first and i want to try it again without exiting the program? I've been using while loop to do it but still no joy. so i decided to do the if statement. But the Array only accept 4 strings then it exit. Any one who can help? TIA.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
template <typename T>
void GetContents(T& Input);
template <typename T>
void DisplayContents(const T& Input);
int main()
{
int PASS = 0;
// To Display the unsorted and sorted Book Titles
std::vector<std::string> books;
GetContents(books);
std::cout << "\nMy original library (number of books: " << books.size() << "):\n\n";
DisplayContents(books);
std::sort(books.begin(), books.end());
std::cout << "\nMy sorted library (number of books: " << books.size() << "):\n\n";
DisplayContents(books);
std::cout << "Press 1 to try again, else to quit: ";
std::cin >> PASS;
std::cout << "\n";
if (PASS == 1)
{
GetContents(books);
}
else
{
return 0;
}
// to input All book titles
template <typename T>
void GetContents(T& Input)
{
const int MAX = 5;
string bookName;
std::cout << "Enter a Book Titles:\n> ";
for (int i = 0; i < MAX; i++)
{
std::getline(std::cin, bookName);
Input.push_back(bookName);
std::cout <<">";
}
}
//Display All input book titles
template <typename T>
void DisplayContents(const T& Input)
{
for (auto iElement : Input)
{
std::cout << iElement << '\n';
}
std::cout << '\n';
system("pause");
}

Related

Forwarding rvalue into a thread where it is further forwarded to another function

I'm trying to create a deadline class which starts a thread in its constructor. Once the deadline (a time priod) occur within a thread, a function provided as argument should be executed.
The class'es contructor takes that function as a parameter, as well as arguments of that function.
I can provide parameter by value and by reference, but when providing rvalue as parameter, I get compile error, which I don't know how to solve.
#include <thread>
#include <atomic>
#include <future>
#include <iostream>
#include <iomanip>
namespace {
std::atomic<bool> running{true};
template <typename Period>
using duration = std::chrono::duration<int64_t, Period>;
template <typename PromiseType = void>
class deadline {
std::promise<PromiseType> stopper_{};
std::thread thread_{};
public:
deadline() = delete;
template <typename Period, typename Function, typename... Args>
explicit deadline(const duration<Period> &interval, Function&& func, Args&&... args) noexcept {
if (interval.count() > 0) {
std::future<PromiseType> future{ stopper_.get_future() };
using tuple = std::tuple<std::decay_t<Args>...>;
auto decay_copied = std::make_unique<tuple>(std::forward<Args>(args)...);
thread_ = std::thread(
[](std::future<PromiseType>&& future,
const duration<Period> &interval,
Function&& func, decltype(decay_copied)&& params) {
if (future.wait_for(interval) == std::future_status::timeout) {
std::apply(func, *params);
}
}, std::move(future), std::cref(interval), std::forward<Function>(func), std::move(decay_copied));
}
}
deadline(const deadline&) = delete;
deadline(deadline&&) = delete;
deadline& operator=(const deadline&) = delete;
deadline& operator=(deadline&&) = delete;
~deadline() {
stopper_.set_value();
if (thread_.joinable()) {
thread_.join();
}
}
};
}
auto main() -> int {
std::string test1{"test 1"};
std::string test2{"test 2"};
std::string test3{"test 3"};
std::string test4{"test 4"};
using namespace std::chrono_literals;
deadline dl(5s,
[](
std::string test_1
, const std::string& test_2
, std::string& test_3
, std::string&& test_4
) {
std::cout << test_1 << '\n';
std::cout << test_2 << '\n';
std::cout << test_3 << '\n';
test_3 += " modified";
std::cout << test_4 << '\n';
running = false;
}
, test1
, std::cref(test2)
, std::ref(test3)
, std::move(test4)
//, "test 4"
);
while (running.load()) {
std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
std::cout << test1 << '\n';
std::cout << test2 << '\n';
std::cout << test3 << '\n';
std::cout << test4 << '\n';
return 0;
}
The code could be checked here also:
https://godbolt.org/z/fx3a4szoY
Any help is appreciated.
Thanks!
When providing a const value (also a rvalue), it works, it doesn't work if I use std::move.

How to print eight words to a line using vector<string>?

I was trying to make a program write 8 words to a line after a user enter their sentence.Its only printing words that have been typed in and i don't have a clue how to make it type 8 words to a line.
#include <iostream>
#include <vector>
#include <string>
#include <cctype>
using namespace std;
vector<string> sentence;
string sente = "";
void print(string, string);
template<typename T>
void print(vector<T>& v, string)
{
cout << "Enter your sentence " << endl;
getline(cin, sente);
sentence.push_back(sente);
for (auto const elem: sentence)
{
cout << elem;
}
}
int main()
{
print(sentence,sente);
}
Using global variables is generally not a good practice.
Also you don't need a extra vector for your use case.
Take a look at the following code, where you can smartly make use of istringstream for your use case:
#include <iostream>
#include <string>
#include <sstream>
void print()
{
std::string sente;
std::cout << "Enter your sentence " << std::endl;
getline(std::cin, sente);
// Used to split string around spaces.
std::istringstream ss(sente);
int wordCountPerLine = 0;
int requiredWordsPerLine = 8;
// Traverse through all words
do {
// Read a word
std::string word;
ss >> word;
// Print the read word
std::cout << word << " ";
wordCountPerLine++;
if(wordCountPerLine % requiredWordsPerLine == 0){
std::cout<<std::endl;
wordCountPerLine = 0;
}
// While there is more to read
} while (ss);
}
int main()
{
print();
}
Feel free to ask any doubts.

How to Iterate over a map <string, vector<string>> m

I need to write a table from a file in a map > m.
I have 3 strings (state, next_states, outputs)
and i want to write them like this (I know it's not correct)
for (i=0; i < 5; i++)
{
//here I have a code, the 3 strings change for every line
for(j=0; j<m.size(); j++)
{
m[i][j] = "state" + "next_states" + "outputs";
}
}
I thought maybe with iterator it would be better but i don't know how to do it.
Here is how you can iterate through your map:
#include <iostream>
#include <map>
#include <vector>
#include <string>
int main()
{
std::map <std::string, std::vector<std::string>> m =
{
{"1", {"a", "b"}},
{"2", {"c", "d", "e"}},
};
for (auto& element : m)
{
std::cout << element.first << " : ";
for (auto& str : element.second)
{
std::cout << str << "; ";
}
std::cout << std::endl;
}
}

std::thread context of execution (c++14)

The problem appear when an in/out variable of a function called by std::thread changes the value during the execution...
Function:
static int func(stThread_t *&pStThread)
Parameters
pStThread: It´s a struct that has a pointer to std::thread and other variables (some flags)
typedef struct stThread {
stThread() noexcept {...};
stThread(const stThread &cRigth) noexcept {...};
stThread & operator = (const stThread &cRigth) noexcept {...};
std::thread *pThread;
volatile bool bBegin;
volatile bool bEnd;
} stThread_t;
The function func print the address of the std::thread of the parameter
pStThread and the thread id
func before 1785280 this_id 21968
after making an this_thread::sleep for 2 seconds, it print it again
func afer ... this_id 21968
static int func(stThread_t *&pStThread) {
std::thread::id this_id = std::this_thread::get_id();
long long p_begin = (long long)pStThread;
std::cout << "func before " << std::to_string(p_begin) << " this_id " << this_id << "\n";
std::cout.flush();
pStThread->bBegin = true;
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
this_id = std::this_thread::get_id();
long long p_end = (long long)pStThread;
std::cout << "func afer " << std::to_string(p_end) << " this_id " << this_id << "\n";
std::cout.flush();
pStThread->bEnd = true;
return 1;
};
The address of the pointer to std::thread it´s changed (corrutped, deleted..?)
The pStThread is pushing_back of a list of pointer struct stThread_t
std::list<stThread_t*> listOfThreads;
listOfThreads.push_back(pStThread);
I read about std::move, but does not work with pointers
At the end there is a thread "garbage collector" that it is trying to erase all the threads pending of execution.
Full code here
#include <string>
#include <list>
#include <vector>
#include <map>
#include <thread>
#include <mutex>
#include <atomic>
#include <iostream>
typedef struct stThread {
stThread() noexcept {
pThread = NULL;
bBegin = false;
bEnd = false;
};
stThread(const stThread &cRigth) noexcept {
this->pThread = cRigth.pThread;
this->bBegin = (bool)cRigth.bBegin;
this->bEnd = (bool)cRigth.bEnd;
};
stThread & operator = (const stThread &cRigth) noexcept {
this->pThread = cRigth.pThread;
this->bBegin = (bool)cRigth.bBegin;
this->bEnd = (bool)cRigth.bEnd;
return *this;
};
std::thread *pThread;
volatile bool bBegin;
volatile bool bEnd;
} stThread_t;
class CMain
{
public:
typedef std::list<stThread_t*> MyList_threads;
MyList_threads listOfThreads;
public:
CMain() {
std::cout << std::boolalpha << "Ex1 is move-constructible? "
<< std::is_move_constructible<stThread_t>::value << '\n'
<< "Ex1 is trivially move-constructible? "
<< std::is_trivially_move_constructible<stThread_t>::value << '\n'
<< "Ex1 is nothrow move-constructible? "
<< std::is_nothrow_move_constructible<stThread_t>::value << '\n'
<< "Ex2 is trivially move-constructible? "
<< std::is_trivially_move_constructible<stThread_t>::value << '\n'
<< "Ex2 is nothrow move-constructible? "
<< std::is_nothrow_move_constructible<stThread_t>::value << '\n';
};
static int func(stThread_t *&pStThread) {
std::thread::id this_id = std::this_thread::get_id();
long long p_begin = (long long)pStThread;
std::cout << "func before " << std::to_string(p_begin) << " this_id " << this_id << "\n";
std::cout.flush();
pStThread->bBegin = true;
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
this_id = std::this_thread::get_id();
long long p_end = (long long)pStThread;
std::cout << "func afer " << std::to_string(p_end) << " this_id " << this_id << "\n";
std::cout.flush();
pStThread->bEnd = true;
return 1;
};
int _createThreads() {
for (int iIdx = 0; (iIdx < 5); iIdx++) {
stThread_t *pStThread = new stThread_t;
pStThread->pThread = new std::thread(&CMain::func,
std::ref(pStThread));
if (pStThread) {
do {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
} while (!pStThread->bBegin);
listOfThreads.push_back(pStThread);
std::string sLog;
sLog = "\nlistOfThreads.push_back " + std::to_string((long long)pStThread) + "\n";
std::cout << sLog;
std::cout.flush();
}
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
return 1;
};
int _main() {
_createThreads();
std::thread thread_collector([=]() {
bool bEnd = false;
MyList_threads::iterator it;
it = listOfThreads.end();
do {
stThread_t *pStThread = NULL;
if (it == listOfThreads.end()) {
it = listOfThreads.begin();
if (it == listOfThreads.end()) bEnd = true;
}
else it++;
if (it != listOfThreads.end()) {
if ((*it)->bEnd) {
pStThread = *it;
listOfThreads.erase(it);
it = listOfThreads.begin();
}
}
if (pStThread) {
if (pStThread->pThread) {
if (pStThread->pThread->joinable()) {
pStThread->pThread->join();
std::cout << " element deleted " << std::to_string((long long)pStThread) << "\n";
std::cout.flush();
}
delete pStThread->pThread;
pStThread->pThread = NULL;
}
delete pStThread;
}
pStThread = NULL;
std::this_thread::sleep_for(std::chrono::milliseconds(1));
} while (!bEnd);
});
if (thread_collector.joinable()) {
thread_collector.join();
}
return 1;
};
};
int main()
{
CMain _main;
_main._main();
return 0;
}
You have a rather straightforward bug, that is mostly unrelated to threading:
(1) func takes a reference to an stThread_t*.
static int func(stThread_t *&pStThread);
(2) You pass in a reference to pStThread
std::thread(&CMain::func,std::ref(pStThread));
(3) Which is a local variable whose lifetime ends as soon as the loop iteration is finished
for (int iIdx = 0; (iIdx < 5); iIdx++) {
stThread_t *pStThread = new stThread_t;
//...
}
(4) And consequently, you get undefined behavior when your function tries to access the object after it is destroyed. (be careful! here, "the object" refers to the pointer in question, not the object the pointer is pointing to)
It's unclear why you insist on passing pStThread by reference; your function doesn't actually modify the pointer (just what's pointed to), and you don't seem to intend to do any of the things that such a device is actually good for.

Why this small c++11 multi threaded program giving segmentation fault

why this program giving seg fault. I tried figuring out the issue using gdb, but no luck.
#include <iostream>
#include <condition_variable>
#include <thread>
#include <chrono>
using namespace std;
condition_variable cv;
mutex cv_m;
mutex m;
int count = 0;
#define COUNT_DONE 10
#define COUNT_HALT1 3
#define COUNT_HALT2 6
void functionCount1()
{
for(;;)
{
m.lock();
count++;
cout << "Counter value functioncount1: " << count << endl;
m.unlock();
if(count >= COUNT_DONE)
return;
}
}
void functionCount2()
{
for(;;)
{
m.lock();
count++;
cout << "Counter value functionCount2: " << count << endl;
m.unlock();
if(count >= COUNT_DONE) return;
}
}
int main()
{
thread t1(functionCount1), t2(functionCount2);
t1.join();
t2.join();
return 0;
}
Your program has undefined behavior: the accesses to count outside the mutex in functionCount1 and functionCount2 are data races. With the UB corrected, it seems fine:
#include <iostream>
#include <mutex>
#include <thread>
using namespace std;
mutex m;
int count = 0;
#define COUNT_DONE 10
void functionCount(const char* name)
{
for(;;)
{
m.lock();
auto c = ++count;
m.unlock();
cout << "Counter value " << name << ": " << c << endl;
if(c >= COUNT_DONE)
return;
}
}
int main()
{
thread t1(functionCount, "functionCount1"), t2(functionCount, "functionCount2");
t1.join();
t2.join();
}
or if you want to be "clever" and confuse your code reviewers:
void functionCount(const char* name)
{
for(;;)
{
auto c = (std::lock_guard<std::mutex>(m), count++);
cout << "Counter value " << name << ": " << c << endl;
if(c >= count_done)
break;
}
}

Resources