Hi, I am new in multi-threading, So trying to solve below broblem.
1. create 3 thread
2. declare global variable i, one thread will increment i by 2
3. second thread will decrement i by 1
4. third will print i
so print 20 value of i in order
You should have some significant class notes about
1. creating threads
2. synchronizing threads
Modern C++ does threads, but most courses are still stuck on POSIX threading on their Unix / SunOS / Linux whatever school systems.
The next step is to write some simple programs. Recommended order:
1. Create a program that produces more than one thread. Each thread should just print “Hello world!” to standard output. 2. Modify the program so that each thread prints a different thing to standard output. 3. Modify the program to enforce an order on the threads, so that the output is not scrambled.
Etc.
Learning this stuff is work. Write code. Compile. Run. Modify. Repeat.
and pay attention with your test programs and what really happens. What isn't well taught at all is that cooking up threads that wait on each other all the time is often no faster or better than just having normal single threaded code. This problem is critical for practice and great for understanding, but its an example as well overengineering the problem. Threads should make your code better somehow, because they add a fair amount of complexity to writing, maintaining, and debugging the code, so you need some pros to offset those cons. With practice and understanding you will see what I mean; for now just be tangentially aware of what is going on as you learn.
C++ provides the threrad library.
we need to use:
1. thread class object from #include <thread>
2. And for synchronization #include <condition_variable>