learning threading and other topics

Hello, I am trying to learn C++ to get a career in software development, my background is a Sr DBA, but want to jump to the other side, the fun side ;)

anyways, i am learning multithreading,why... just want to learn, curious and always wanting to learn, i know its an advance topic, so i am asking 3 questions, and 1 of them something is wrong with my code:

1. I am learning c++ to learn and do graphics and/or networking programs in c++ but would be awesome to work on game engines and games... I am not sure if learning threading will help but not sure if required or recommended?

2. What other good topics to learn in, so far i have learned:
Lambda
Pointers
constructors
destructors
classes
memory, difference between stack and heap
Templates
linking dynamic and static libraries
Linking compiled headers
smart pointers
overloading operators
macros (basics)

going to get into L and R values and map, but now on threading... anything else will help me for graphics, networking etc. to get a c++ programming job?

3. below is the code i am working on, 1 problem is the rand(), i should use srand() with time.h of course, but how can i assign it to an int or float and pass it as a variable? code is below, let me know your thoughts, if i am on a good path to becoming a c++ developer?



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

void DepositMoney(int* acctb, int r)
{
	for (int x = 0; x < 50; x++)
	{
		std::this_thread::sleep_for(1s);
		mu.lock();

		(*acctb) += r;

		std::cout << "Deposit Amount: " << r << "\n";
		std::cout << "Deposit NEW balance: $ " << (*acctb) << "\n";
		mu.unlock();
	}
}


void WithdrawalMoney(int* acctb, int r)
{
	for (int x = 0; x < 50; x++)
	{
		std::this_thread::sleep_for(1s);
		mu.lock();

		(*acctb) -= r;

		std::cout <<"Withdrawal Amount: " << r << "\n";
		std::cout << "Withdrawal NEW balance: $ " << (*acctb) << "\n";
		mu.unlock();
	}
}

int main()
{
	int acctbalance = 1000;

	int r = rand() % 50 + 1;
	
	std::thread worker1(DepositMoney, (&acctbalance), r);
	std::cout << "Thread 1: " << worker1.get_id() << "\n";

	std::thread worker2(WithdrawalMoney, (&acctbalance), r);
	std::cout << "Thread 2: " << worker2.get_id() << "\n";

	worker1.join();
	worker2.join();
	
	std::cout << "Final Account Balance: $ " << acctbalance << "\n";
	

	std::cin.get();
}
For thread locking, look up std::lock. In modern C++ (and even older C++), using RAII is key. That way you don't have to remember to unlock, and won't suffer issues in the presence of exceptions.

More to the point, get a copy of Stroustrup's C++ Principles and Practice, or something similar.

For a larger treatment of C++ 17 (more than beginner), consider Josuttis on the STL, and his recent C++ 17 work.

Josuttis makes clear the issues about LValues, RValues, XValues and more.

I'm short on time, more later perhaps.

Topic archived. No new replies allowed.