Race Conditions Explained With An Example | C Programming Tutorial

  Рет қаралды 10,838

Portfolio Courses

Portfolio Courses

2 жыл бұрын

An explanation of what a race condition with an example of one created using C, where we simulate multiple threads accessing shared bank account state. Source code: github.com/portfoliocourses/c.... Check out www.portfoliocourses.com to build a portfolio that will impress employers!

Пікірлер: 13
@pauldhartley
@pauldhartley 2 ай бұрын
Would this be overcome if the write_balance function was also passed the balance before the change and the new balance adjusted for any change in the read balance. So the second call to write balance would be passed 200 and 400 - it would then compare the first 200 with the current balance (500) and modifty the balance accordingly. I haven't tried this out,
@mongraal2272
@mongraal2272 2 жыл бұрын
almost 2k bro!
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
Very exciting! The support from people has been incredible. :-)
@matheusb.r.461
@matheusb.r.461 2 ай бұрын
Hi. I eventually get 300 printed. I think in this case the second thread (thread2) is executed first then thread1 writes 300. Is that correct?
@y.8901
@y.8901 2 ай бұрын
Hi, I suppose that your explanation is true, since each thread is scheduled independently (and both in parallel !), it is possible that thread1 is the last being executed
@windowsbuilderthegreat3121
@windowsbuilderthegreat3121 2 жыл бұрын
How come you don’t have to include the header file in the bank.c file
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
It has to do with how C programs are compiled. But basically, the bank.c file does not need anything from the bank.h file. It can just define the functions, and that's OK, it doesn't need the function declarations in the .h file to do this. The d.c file with the main function *does* need to know the function declarations from the bank.h file so it needs to include it in order to use them. The d.c and bank.c files get compiled separately and then "linked" during the linker stage of compilation, which is where everything comes together to produce the executable program. Maybe on day I can make a video on C program compilation. 🙂
@windowsbuilderthegreat3121
@windowsbuilderthegreat3121 2 жыл бұрын
@@PortfolioCourses thanks for clarifying
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
@@windowsbuilderthegreat3121 You're welcome!
@RaPTeN777
@RaPTeN777 Жыл бұрын
Am I the only one that realizes this function in github doesn't work. There are variables outside of scope that appear to not have any easy solution to allow the program to run. unless i'm missing something. ERROR warning: implicit declaration of function 'write_balance' [-Wimplicit-function-declaration] 63 | write_balance(account_balance);
@PortfolioCourses
@PortfolioCourses Жыл бұрын
It sounds like you're missing something when compiling it. :-) Are you missing the #include of bank.h at the top of main? That would be the simplest explanation.
@RaPTeN777
@RaPTeN777 Жыл бұрын
Honestly I was shocked I even got a response. I want to clarify that I am super new at this and am attempting to recreate this using Visual studio Code. I messed up the first time as I named all of the initial typed files the wrong extension. That said after using the github files following along with the video I am receiving a weird error still. So basically it pushes the initial value of =0 but doesn't allow the 500 value to come through because of an error relating to "(.text+0x8c): undefined reference to `read_balance'". I am not sure exactly why this is occurring. The read balance it is referring to is the second read_balance in the line: 46, 57 as well as the line 63. Idk if windows and C are just not friends and the linux mac is your friend on this one but i am not sure exactly why I can't seem to get this sorted out. I've tried everything moving { brackets around } to correct the defined functions. Any advice or place I should look at that might help point to a solution would be appreciated.
@PortfolioCourses
@PortfolioCourses Жыл бұрын
Oh OK, pthreads are a Unix/Linux/Mac way of dong threads, so that is likely going to be a problem if you want to use this code in Windows. It's technically possible to use pthreads in Windows in some ways, but Windows has its own threading functions. If you just want to play with the code for learning purposes, it should work in an online compiler like this one: www.onlinegdb.com/online_c_compiler. These compilers tend to use gcc/linux in the backend. :-)
Make A File Directory With mkdir() | C Programming Tutorial
8:16
Portfolio Courses
Рет қаралды 9 М.
Mutex VS Semaphore In C++
7:17
CppNuts
Рет қаралды 10 М.
One moment can change your life ✨🔄
00:32
A4
Рет қаралды 31 МЛН
File Access Basics | C Programming Tutorial
24:05
Portfolio Courses
Рет қаралды 88 М.
Mutex Introduction (pthreads) | C Programming Tutorial
10:19
Portfolio Courses
Рет қаралды 19 М.
Function-like Macros | C Programming Tutorial
24:12
Portfolio Courses
Рет қаралды 18 М.
Function Pointers | C Programming Tutorial
18:31
Portfolio Courses
Рет қаралды 57 М.
The fork() function in C
11:16
CodeVault
Рет қаралды 357 М.
WHY did this C++ code FAIL?
38:10
The Cherno
Рет қаралды 225 М.
array vs &array Pointers Difference Explained | C Programming Tutorial
17:38
Race Condition vs Data Races in Java
9:58
Defog Tech
Рет қаралды 43 М.
Introduction To Threads (pthreads) | C Programming Tutorial
13:39
Portfolio Courses
Рет қаралды 87 М.
坐车一定要系上安全带#海贼王#路飞
0:26
路飞与唐舞桐
Рет қаралды 6 МЛН
¿Puedo conseguir chocolate MrBeast y PRIME?
0:20
McBang ASMR
Рет қаралды 10 МЛН
joga água e pula #funny #funnyvideo #shorts
0:17
Mundo de Alícia e Ana Clara
Рет қаралды 7 МЛН
Машина Жириновского.  #shorts Лиса рулит
0:52
Лиса рулит shorts
Рет қаралды 3,8 МЛН
Love conquers all rules?
0:26
Den Do It
Рет қаралды 3,4 МЛН