Implementation of Threads

Jan 19, 2022 at 7:20am
There is a program that I am doing on a Parking system in which, I want to implement a background thread that reads data from a file and gives a real-time output of the count of data on the console.

can someone please help with some kind of example because I am a bit confused that how to implement this?

Thank you
Last edited on Jan 19, 2022 at 7:21am
Jan 19, 2022 at 7:33am
See this for how to start a thread:

http://www.cplusplus.com/reference/thread/thread/?kw=thread

Though I doubt that a thread is usefull here. Threads let you do two or more things at the same time, but is usually not good for 'speed up' things...
Jan 19, 2022 at 7:52am
Given the very basic nature of your previous thread, I doubt you're anywhere near ready for thread programming.
https://www.cplusplus.com/forum/beginner/281787/

Jan 19, 2022 at 8:38am
thank you @coder777
Jan 19, 2022 at 9:41am
Here you go @Viraj, enjoy this.
https://en.wikipedia.org/wiki/Heisenbug

When you've dug yourself a massive hole for yourself, I'll be here with a "told you so".
Jan 19, 2022 at 10:48am
Given the very basic nature of your previous thread, I doubt you're anywhere near ready for thread programming.
We all have to start somewhere.
Jan 19, 2022 at 2:42pm
Though I doubt that a thread is usefull here. Threads let you do two or more things at the same time, but is usually not good for 'speed up' things...


I will respectfully disagree.
Threads are great at speeding things up, if the thing you are attacking is a good candidate for it. A dumb example that is easy to understand is a linear search through unsorted data. If you have a shiny new I9 CPU with 20 cores, you can divide your data pile into 20 chunks and search each one 20 times faster.
There are a lot of algorithms that benefit from a simple division of labor approach with the high core count on modern hardware.

I do agree that a thread is not useful here. If it were a live stream and not a file, maybe, or a streamed file that is being written as you are reading it (more complexity to handle!) on a SSD, "maybe" a thread would help. Hard to say without details.
Last edited on Jan 19, 2022 at 2:43pm
Jan 19, 2022 at 2:52pm
In addition to the above by @jonnin, there's also the possibility of using an external program to follow the file in real-time.
https://www.geeksforgeeks.org/tail-command-linux-examples/

You don't have to solve all your problems in a single app.
Jan 19, 2022 at 4:53pm
Thank you @kbw, @salem c, @jonnin.

Jan 30, 2022 at 9:25pm
hello,

you can take a look at this example:

https://mockstacks.com/Cpp_Threading

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <string>
#include <iostream>
#include <thread>

using namespace std;

// The function we want to execute on the new thread.
void task1(string msg)
{
    cout << "task1 says: " << msg;
}

int main()
{
    // Constructs the new thread and runs it. Does not block execution.
    thread t1(task1, "Hello");

    // Do other things...

    // Makes the main thread wait for the new thread to finish execution, therefore blocks its own execution.
    t1.join();
}
Topic archived. No new replies allowed.