std::thread - read from file line by line - c++

I would like to read in parallel line by line from output file. Every thread read one line then work with data. In the mean time next thread have to read the next line.
std::ifstream infile("test.txt");
std::mutex mtx;
void read(int id_thread){
while(infile.good()){
mtx.lock();
std::string sLine;
getline(infile, sLine);
std::cout << "Read by thread: " << id_thread;
std::cout << sLine << std::endl;
mtx.unlock();
}
}
void main(){
std::vector<std::thread> threads;
for(int i = 0; i < num; i++){
threads.push_back(std::thread(parallelFun, i));
}
for(auto& thread : threads){
thread.join();
}
return 0;
}
When i run this code i get this:
First thread read all lines. How can i make it happen that every thread read one line?
EDIT
As mentioned in the comments all i needed to do was bigger test file.
Thanks guys!

I would change the loop into
while(infile.good()){
mtx.lock();
std::string sLine;
getline(infile, sLine);
mtx.unlock();
std::cout << "Read by thread: " << id_thread;
std::cout << sLine << std::endl;
}
Your std::cout stuff is the busy part of your test loop which you want to exchange for real code later. This gives the other thread time to kick in. Furthermore make your test file large. It is not uncommon that thread initialization takes some time in which the first thread eats all the data.

If you want your 5 threads to read exactly every 5th line you must synchronize the reads, so each thread must know that the previous has finished reading its part. This requirement potentially impose a huge inefficiency as some threads could be waiting a long time for the previous in order to run.
Concept code, untested use at own risk.
Lets first make a default class to handle atomic locks. We align it to avoid false sharing and the associated cache ping-pong.
constexpr size_t CACHELINESIZE = 64; // could differ on your architecture
template<class dType>
class alignas(CACHELINESIZE) lockstep {
std::atomic<dType> lock = dType(0);
public:
// spinlock spins until the previous value is prev and then tries to set lock to value
// until success, restart the spin if prev changes.
dType Spinlock(dType prev = dType(0), dType next = dType(1)) {
dType expected = prev;
while (!lock.compare_exchange_weak(expected, next)) { // request for locked-exclusiv ~100 cycles?
expected = prev; // we wish to continue to wait for expected
do {
pause(); // on intel waits roughly one L2 latency time.
} while(lock.load(std::memory_order_relaxed) != prev); // only one cache miss per change
}
return expected;
}
void store(dType value) {
lock.store(value);
}
};
lockstep<int> lock { 0 };
constexpr int NoThreads = 5;
std::ifstream infile("test.txt");
void read(int id_thread) {
locks[id_thread].lock = id_thread;
bool izNoGood = false;
int next = id_thread;
while(!izNoGood){
// get lock for next iteration
lock.spinlock(next, next); // wait on our number
// moved file check into locked region
izNoGood = !infile.good();
if (izNoGood) {
lock.store(next+1); // release next thread to end run.
return;
}
std::string sLine;
getline(infile, sLine);
// release next thread
lock.store(next+1);
// do work asynchronous
// ...
// debug log, hopefully the whole line gets written in one go (atomic)
// but can be in "random" order relative to other lines.
std::cout << "Read by thread: " << id_thread << " line no. " << next
<< " text:" << sLine << std::endl; // endl flushes cout, implicit sync?
next += NoThreads; // our next expected line to process
}
}
void main() {
std::vector<std::thread> threads;
for(int i = 0; i < NoThreads; i++) {
threads.push_back(std::thread(parallelFun, i));
}
for(auto& thread : threads){
thread.join();
}
return 0;
}

Just in case you want each thread to read a single line ( which is obvious from your description ) , remove the while loop and then you need to make sure that you have same number of threads as number of lines in file.
To get rid of above constraint you can use boost threadpool.

Related

Pause and resume a c++ function

I'm facing a problem in programmation where I did not find any convenient and fast execution solutions.
I'm trying to implement some kind of state machine : take one byte in entry, process it, change state, loop, etc... The purpose is to process byte stream without requiring any memory buffer (processing byte per byte).
The class should looks like this :
class Decoder {
void next() {
int i = 0;
std::cout << i << "\n";
i++;
yield(); // pseudo code => should stop the function and save the current state (or simply not freeing allocated variables)
std::cout << i << "\n";
}
};
Decoder decoder = Decoder();
decoder.next(); // print 1
std::cout << "1.5" << "\n"; // print 1.5
decoder.next(); // print 2
A solution could be to create a step property to save the step and then resume with a switch but the performances would be strongly impacted. I would like to know if there is a way to exit the execution of a function and then resume it later ?
To be clear, I don't want to pause the entire program, only a function. Pausing such a function would return to the caller and continue the execution of the program until the next next is called.
Moreover, I would like to avoid thread and std as much (I prefer all environnements code). Finally, if you have any other alternatives to my problem : process byte stream efficiently for the memory, I'm open to your suggestions.
Thanks for your help.
I believe you could achieve just that using these two ways:
Option 1: Member state
Split the state machine object into a separate object, and convert all your local variables to members.
For each step of the way, save a State member, signifying where are you now throughout the execution of your program.
Each time you enter next() check your state against a switch and call the designated inner method for that step.
Each such step method simulates code execution between to consecutive yields.
struct Decoder {
void next() {
switch (_step) {
case s1:
step1();
_step = s2;
return;
case s2:
step2();
_step = s1;
return;
default:
return; // handle error...
}
}
private:
enum Step { s1, s2 };
Step _step = s1;
int _i = 1;
void step1() {
std::cout << _i << "\n";
_i++;
}
void step2() {
std::cout << _i << "\n";
}
};
int main() {
Decoder decoder = Decoder();
decoder.next(); // print 1
std::cout << "1.5" << "\n"; // print 1.5
decoder.next(); // print 2
}
Option 2: Thread and signalling
Use a thread, which you could of course run using native APIs (e.g. pthread_create on POSIX platforms).
Inside your thread, every time you want to yield, wait on a conditional variable, e.g:
struct Decoder {
Decoder() {
_thread = std::thread { &Decoder::worker, this };
}
~Decoder() {
_thread.join();
}
void next() {
std::lock_guard<std::mutex> lock(_mutex);
_work = true;
}
private:
void wait() {
std::unique_lock<std::mutex> lock(_mutex);
_cond.wait(lock, [this](){return _work;});
}
void worker() {
wait();
int i = 0;
std::cout << i << "\n";
i++;
wait();
std::cout << i << "\n";
}
std::thread _thread;
std::mutex _mutex;
std::condition_variable _cond;
bool _work = false;
};
int main() {
Decoder decoder;
decoder.next(); // print 1
std::cout << "1.5" << "\n"; // print 1.5
decoder.next(); // print 2
}

Having set amount of thread to work as consumers

I have created a producer / consumer code as following
class CTest{
public:
void producer( int i ){
unique_lock<mutex> l(m);
q.push(i);
if( q.size() )
cnd.notify_all();
}
void consumer(int i ){
unique_lock<mutex> l(m);
while( q.empty() ){
cnd.wait(l );
}
if( q.empty())
return;
cout << "IM AWAKE :" << i << endl;
int tmp = q.front();
q.pop();
l.unlock();
cout << "Producer got " << tmp << endl;
}
void ConsumerInit( int threads ){
for( int i = 0; i < threads; i++ ){
thrs.push_back(thread(&CTest::consumer, this ,i));
}
}
void waitForTHreads(){
for( auto &a : thrs )
a.join();
}
void printQueue(){
while( ! q.empty()){
int tmp = q.front();
q.pop();
cout << "Queue got " << tmp << endl;
}
}
private:
queue<int> q;
vector<thread> thrs;
mutex m;
condition_variable cnd;
};
and main
int main(){
int x;
CTest t;
int counter = 0;
while( cin >> x ){
if( x == 0 ){
cout << "yay" << endl;;
break;
}
if( x == 1)
t.producer(counter++);
if( x == 2 )
t.ConsumerInit(5);
}
t.waitForTHreads();
t.printQueue();
return 0;
}
What this code does it , when user inputs "1" it will add number to the queue ,when user inputs "2" , 5 threads are spawned to retrieve data from queue and print it. However my problem is as followng , when i input
6 numbers , only 5 of them are printed due to fact that only 5 threads are spawned , what i want to do is thread to retrieve a data from queue , print int, and then again waiting if it can print another data. This way all N > 5 numbers would pri printed with just 5 threads.
My question is , what is standard way how to achieve this? I read few documens but didnt fint/cannot think of good solution. How are problems like this solved?
when i try to create simple thread pool :
void consumer(int i ){
while(true){
{
unique_lock<mutex> l(m);
while( q.empty() ){
cnd.wait(l );
}
if( q.empty())
return;
cout << "IM AWAKE :" << i << endl;
int tmp = q.front();
q.pop();
cout << "Producer " << i << " got " << tmp << endl;
} //consumer(i);
}
}
and input N number all numbers are processed by one thread.
Thanks for help!
The current version of consumer can only read one value before exiting. In order to read more, it must loop, and this leads to your second version of consumer which has two problems:
Consumption here is so quick that the first thread into the queue can consume the whole queue within its timeslice (or however CPU is being allocated). Insert a yield or a sleep to force the OS to switch tasks.
The mutex is not unlocked so no other threads are able to get in.
Fortunately you aren't creating the threads until you need them and they terminate after the queue is empty so the whole deal with conditional_variable can go out the window.
void consumer(int i)
{
unique_lock<mutex> l(m);
while (!q.empty())
{
int tmp = q.front();
q.pop();
cout << i << " got " << tmp << endl;
// note: In the real world, locking around a cout is gross. cout is slow,
// so you want the unlock up one line. But...! This allows multiple threads
// to write to the consle at the same time and that makes your output
// look like it was tossed into a blender, so we'll take the performance hit
l.unlock(); // let other threads have a turn
this_thread::yield();
l.lock(); // lock again so the queue can be safely inspected
}
}
If you need to go with the threadpool approach, things get a little messier and the condition variable makes a return.
void consumer(int i)
{
while (true)
{
unique_lock<mutex> l(m);
if (q.empty())
{
cnd.wait(l);
}
if (!q.empty()) // OK. We got out of the conditional wait, but have
// other threads sucked the queue dry? Better check.
{
int tmp = q.front();
q.pop();
cout << i << " got " << tmp << endl;
}
l.unlock();
this_thread::yield();
}
}
An atomic<bool> terminated may be helpful to allow an orderly shutdown while (true) does not allow for.
In general, without going into code details, a threadpool is created and the threads are put in a wait state (waiting on one or more events / signals, or in your case condition_variable cnd;) - I'm used to work with events, so I'll use that in the following text, but a condition_variable should work in a similar way.
When a task is added to the queue, a task-event is set/fired and one ore more threads wake up (depending on the event (single / multi)).
When a thread wakes up, it checks (with a lock) if there is a task available, if available, executes the task and when finished checks again (!) if there are more tasks waiting. (because when you add 8 tasks in one go, 5 threads become active, so they need to check if there are more tasks after finishing their first one.
If there are no jobs left, the thread goes back in the wait state (waiting for a next job, or a quit event).
When quitting the application, another, say quit-event, is set for all threads (you can't just wait for the threads to finish, because the threads themselves are waiting on an event to do some work) -- or you could fire the same event, and first set a volatile variable, which the threads should then first check on any event to see if they need to quit, or do another job. Then you can wait for the threads to 'come home'.
A lock should be held as short as possible.
As for your code:
void producer( int i ){
unique_lock<mutex> l(m);
q.push(i);
if( q.size() )
cnd.notify_all();
}
Here the lock is held longer than needed (and perhaps too long). You also just pushed a value, so q will not be empty (no need to check). Since you only add one item (task), only one thread should be woken up (so notify_one() should be fine here).
So you should: lock, push, unlock, notify - instead of unlock, you can place the lock and push inside brackets, which will trigger an unlock in the unique_lock<> destructor.
void consumer(int i ){
unique_lock<mutex> l(m);
while( q.empty() ){
cnd.wait(l );
}
if( q.empty())
return;
cout << "IM AWAKE :" << i << endl;
int tmp = q.front();
q.pop();
l.unlock();
cout << "Producer got " << tmp << endl;
}
Here you should lock, check queue, pop if there is a task, unlock, if no task, put the thread in a wait state again, else do work with the popped value (after unlocking), and then check again if there is more work to do. Normally it is not a good idea to call cout while the data is locked.. but for a small test you could get away with it, especially because cout needs to be synchronized too (but it would be cleaner to synchronize cout on its own, separate from your data lock).
void printQueue(){
while( ! q.empty()){
int tmp = q.front();
q.pop();
cout << "Queue got " << tmp << endl;
}
}
Make sure your data is locked here too! (although it's only called from main after the threads have finished, the function is in your class, and the data should be locked).

Synchronize threads using mutex

I'm trying to understand C++ Multithreading and synchronize between many threads.
Thus I created 2 threads the first one increments a value and the second one decrements it. what I can't understand why the resulted value after the execution is different than the first one, since I added and subtracted from the same value.
static unsigned int counter = 100;
static bool alive = true;
static Lock lock;
std::mutex mutex;
void add() {
while (alive)
{
mutex.lock();
counter += 10;
std::cout << "Counter Add = " << counter << std::endl;
mutex.unlock();
}
}
void sub() {
while (alive)
{
mutex.lock();
counter -= 10;
std::cout << "Counter Sub = " << counter<< std::endl;
mutex.unlock();
}
}
int main()
{
std::cout << "critical section value at the start " << counter << std::endl;
std::thread tAdd(add);
std::thread tSub(sub);
Sleep(1000);
alive = false;
tAdd.join();
tSub.join();
std::cout << "critical section value at the end " << counter << std::endl;
return 0;
}
Output
critical section value at the start 100
critical section value at the end 220
So what I need is how to keep my value as it's, I mean counter equal to 100 using those two threads.
The problem is that both threads will get into an "infinite" loop for 1 second and they will get greedy with the mutex. Do a print in both functions and see which thread gets the lock more often.
Mutexes are used to synchronize access to resources so that threads will not read/write incomplete or corrupted data, not create a neat sequence.
If you want to keep that value at 100 at the end of execution you need to use a semaphore so that there will be an ordered sequence of access to the variable.
I think, what you want is to signal to the subtracting thread, that you just have sucessfully added in the add thread, and vice versa. You'll have to additionally communicate the information, which thread is next. A naive solution:
bool shouldAdd = true;
add() {
while( alive ) {
if( shouldAdd ) {
// prefer lock guards over lock() and unlock() for exception safety
std::lock_guard<std::mutex> lock{mutex};
counter += 10;
std::cout << "Counter Add = " << counter << std::endl;
shouldAdd = false;
}
}
}
sub() {
while( alive ) {
if( !shouldAdd ) {
std::lock_guard<std::mutex> lock{mutex};
counter -= 10;
std::cout << "Counter Sub = " << counter << std::endl;
shouldAdd = true;
}
}
}
Now add() will busy wait for sub() to do its job before it will try and acquire the lock again.
To prevent busy waiting, you might chose a condition variable, instead of trying to only use a single mutex. You can wait() on the condition variable, before you add or subtract, and notify() the waiting thread afterwards.

Still having race condition with boost::mutex

I am trying an example, which causes race condition to apply the mutex. However, even with the mutex, it still happens. What's wrong? Here is my code:
#include <iostream>
#include <boost/thread.hpp>
#include <vector>
using namespace std;
class Soldier
{
private:
boost::thread m_Thread;
public:
static int count , moneySpent;
static boost::mutex soldierMutex;
Soldier(){}
void start(int cost)
{
m_Thread = boost::thread(&Soldier::process, this,cost);
}
void process(int cost)
{
{
boost::mutex::scoped_lock lock(soldierMutex);
//soldierMutex.lock();
int tmp = count;
++tmp;
count = tmp;
tmp = moneySpent;
tmp += cost;
moneySpent = tmp;
// soldierMutex.unlock();
}
}
void join()
{
m_Thread.join();
}
};
int Soldier::count, Soldier::moneySpent;
boost::mutex Soldier::soldierMutex;
int main()
{
Soldier s1,s2,s3;
s1.start(20);
s2.start(30);
s3.start(40);
s1.join();
s2.join();
s3.join();
for (int i = 0; i < 100; ++i)
{
Soldier s;
s.start(30);
}
cout << "Total soldier: " << Soldier::count << '\n';
cout << "Money spent: " << Soldier::moneySpent << '\n';
}
It looks like you're not waiting for the threads started in the loop to finish. Change the loop to:
for (int i = 0; i < 100; ++i)
{
Soldier s;
s.start(30);
s.join();
}
edit to explain further
The problem you saw was that the values printed out were wrong, so you assumed there was a race condition in the threads. The race in fact was when you printed the values - they were printed while not all the threads had a chance to execute
Based on this and your previous post (were it does not seem you have read all the answers yet). What you are looking for is some form of synchronization point to prevent the main() thread from exiting the application (because when the main thread exits the application all the children thread die).
This is why you call join() all the time to prevent the main() thread from exiting until the thread has exited. As a result of your usage though your loop of threads is not parallel and each thread is run in sequence to completion (so no real point in using the thread).
Note: join() like in Java waits for the thread to complete. It does not start the thread.
A quick look at the boost documentation suggests what you are looking for is a thread group which will allow you to wait for all threads in the group to complete before exiting.
//No compiler so this is untested.
// But it should look something like this.
// Note 2: I have not used boost::threads much.
int main()
{
boost::thread_group group;
boost::ptr_vector<boost::thread> threads;
for(int loop = 0; loop < 100; ++loop)
{
// Create an object.
// With the function to make it start. Store the thread in a vector
threads.push_back(new boost::thread(<Function To Call>));
// Add the thread to the group.
group.add(threads.back());
}
// Make sure main does not exit before all the threads have completed.
group.join_all();
}
If we go back to your example and retrofit your Soldier class:
int main()
{
boost::thread batallion;
// Make all the soldiers part of a group.
// When you start the thread make the thread join the group.
Soldier s1(batallion);
Soldier s2(batallion);
Soldier s3(batallion);
s1.start(20);
s2.start(30);
s3.start(40);
// Create 100 soldiers outside the loo
std::vector<Soldier> lotsOfSoldiers;
lotsOfSoldiers.reserve(100); // to prevent reallocation in the loop.
// Because you are using objects we need to
// prevent copying of them after the thread starts.
for (int i = 0; i < 100; ++i)
{
lotsOfSoldiers.push_back(Solder(batallion));
lotsOfSoldiers.back().start(30);
}
// Print out values while threads are still running
// Note you may get here before any thread.
cout << "Total soldier: " << Soldier::count << '\n';
cout << "Money spent: " << Soldier::moneySpent << '\n';
batallion.join_all();
// Print out values when all threads are finished.
cout << "Total soldier: " << Soldier::count << '\n';
cout << "Money spent: " << Soldier::moneySpent << '\n';
}

c++ Boost Multithread CPU and Memory Usages are slow and high - need help

I am using boost library to create multithread with mutex. I based on some example on google. The multitrheads are working fine, but CPU are 100%, memory usage from task manager are 300K+. My two multithread functions are performed very high data conversion and analysis. I am not sure if I missed somethings. Here are the part of my multithread:
wait(1);
mutex.lock();
boost::thread t1(&YExcel::BasicExcelCell::TTiTraceParserConv,c,TTiAsciiTraceOutputDL.GetBuffer(0));
mutex.unlock();
wait(2);
mutex.lock();
boost::thread t2(&YExcel::BasicExcelCell::TTiTraceParserConv,c,TTiAsciiTraceOutputUL.GetBuffer(0));
mutex.unlock();
t1.join();
t2.join();
wait(2);
wait(1);
mutex.lock();
boost::thread t5(&YExcel::BasicExcelCell::UeAndCellParamParseUL,c,TTiAsciiTraceOutputUL.GetBuffer(0), NumOfLinesUL,GHOSTFILTER);
mutex.unlock();
wait(2);
mutex.lock();
boost::thread t6(&YExcel::BasicExcelCell::UeAndCellParamParseDL,c,TTiAsciiTraceOutputDL.GetBuffer(0), NumOfLinesDL);
mutex.unlock();
t5.join();
t6.join();
Do I need to do somethings inside those functions? Thanks in advance. I was tested with Dell Optilex 745 Pentium D. I am not sure it supports Multi-thread. I had printed hard_concurrent, it showed on 1.
One of the function - summary
int BasicExcelCell::UeAndCellParamParseDL(char *inname, int NumOfRecords)
{
typedef boost::tokenizer <boost::escaped_list_separator<char> > my_tokenizer;
....
....
CString TTiAsciiTraceOutput(inname);
TTiAsciiTraceOutput.Replace(".dat","_raw.txt");
ifstream infile(TTiAsciiTraceOutput.GetBuffer(0));
if (!infile)
{
cout << "Couldn't open file " << TTiAsciiTraceOutput.GetBuffer(0) << " for reading." << endl;
return EXIT_FAILURE;
}
cout << "\nOpened file " << TTiAsciiTraceOutput.GetBuffer(0) << " for reading." << endl << endl;
int lineCount=0;
getline(infile, line);
if (NumOfRecords < 0)
{
cout<<"Number Of Lines to be parsed is not specified!"<<endl;
return 0;
}
while (getline(infile, line) && lineCount <= NumOfRecords)
{
lineCount++;
int FoundCrntiEmpty=0;
my_tokenizer tok(line);
int i = 0;
for (my_tokenizer::iterator it(tok.begin()), end(tok.end()); it != end; ++it)
{
mystr.push_back(*it);
}
int tokcount=0;
int SingleTx=0;
int TxDiv=0;
int tokCountFlag=0;
double MetricCalTemp=0.0;
for (vector < string >::iterator mit(mystr.begin()); mit != mystr.end(); mit++)
{
for (vector < string >::iterator _mit(mystr.begin()); _mit != mystr.end(); _mit++)
{
tokCountFlag++;
if (tokCountFlag==60)
break;//not need to goto the whole iterator
switch (tokCountFlag)
{
case 29:
<<*_mit<<endl;
if (*_mit=="0")
FoundCrntiEmpty=1;
break;
case 58:
//cout<<"Single or Tx " <<*_mit<<endl;
if (*_mit == "0")
SingleTx=1;
else if (*_mit == "1")
TxDiv=1;
break;
}
}
}
}
Another short function on first thread:
void BasicExcelCell::TTiTraceParserConv(char *p_resFile)
{
char ttiTraceParser[512];
char ttiTraceConfig[512];
char cwd[512];
size_t size;
char OrigDLFile[512];
GetModuleFileName(NULL, cwd, 512);
char * CollectTTiTraceAdvance_exe;
CollectTTiTraceAdvance_exe = strstr (cwd,"CollectTTiTraceAdvance.exe");
strncpy (CollectTTiTraceAdvance_exe,"",26);
CString TTiAsciiTraceOutput(p_resFile);
CString ParserExe, TraceConfig;
ParserExe.Format(_T("%s\\BinaryFileParser\\tti_trace_parser_wmp.exe "),cwd);
sprintf(OrigDLFile,"%s",TTiAsciiTraceOutput.GetBuffer(0));
TTiAsciiTraceOutput.Replace(".dat","_raw.txt");
TraceConfig.Format(_T(" %s\\%s %s\\%s"),cwd,OrigDLFile,cwd,TTiAsciiTraceOutput);
cout<<"\n\n****Prepare to generate RawFile!!!"<<endl;
ShellAndWait(ParserExe.GetBuffer(0),TraceConfig.GetBuffer(0),"WAIT",240,1);//4 minutes
ParserExe.ReleaseBuffer(0);
TraceConfig.ReleaseBuffer(0);
}
This doesn't look right to me:
mutex.lock();
boost::thread t6(...);
mutex.unlock();
This only protects the creation of the thread from running concurrently with anything else, it has no effect on the operations carried out by that thread. You'll need to lock the mutex from within the thread function to gain any protection.
In addition, as pointed out in the comments, the waits are suspicious and should not be needed.
Finally, you should (in my opinion) never use mutex.lock/mutex.unlock directly because it's error-prone, especially in the presence of exceptions. Use the RAII tools such as scoped_lock which are provided by Boost.
To get any more information, you'll have to show us what those thread functions are actually doing.