✅ do using the lock object in the below code make seance?

think this i real program that has users will prevent the other users to use _ aqurie in case of readin or writting
c#
internal class Program
{



static int? _aquired;

static object _lock = new object();

static void Main(string[] args)
{
Thread T1 = new Thread(() => {
int aquire = default;

if(int.TryParse(Console.ReadLine(), out aquire))
{

lock (_lock)
{
_aquired = aquire;
}

}
});

T1.Start();

T1.Join();



Thread T2 = new Thread(() => {

lock (_lock)
{
if (_aquired != default)
{
for (int i = 0; i <= _aquired; i++)
{
Console.WriteLine("The Turn = {0}", i);
}

}
}


});

T2.Start();



}


}


}
c#
internal class Program
{



static int? _aquired;

static object _lock = new object();

static void Main(string[] args)
{
Thread T1 = new Thread(() => {
int aquire = default;

if(int.TryParse(Console.ReadLine(), out aquire))
{

lock (_lock)
{
_aquired = aquire;
}

}
});

T1.Start();

T1.Join();



Thread T2 = new Thread(() => {

lock (_lock)
{
if (_aquired != default)
{
for (int i = 0; i <= _aquired; i++)
{
Console.WriteLine("The Turn = {0}", i);
}

}
}


});

T2.Start();



}


}


}
20 Replies
ParaLogia
ParaLogia4mo ago
What do you mean by "the other users"?
steven preadly
steven preadlyOP4mo ago
i mean that if i have a user that is using the program on thread for writing and i have another client that use the same application and he wants to write to the same _acquire then this client will not be able to do so as the thread is locked , the only way that he could write on the _acquire is when the lock got relased
Jimmacle
Jimmacle4mo ago
it doesn't make sense for this code to be using multiple threads at all, it all ends up running synchronously likewise, because thread 1 and thread 2 cannot run at the same time in this code there is no point in using a lock and if they were running at the same time, it would probably be more effective to copy the value of _acquired to a local instead of locking it
steven preadly
steven preadlyOP4mo ago
what id i have a system that i don`t want the user to access specific resource like creating task as an example while other user is doing he same in that case i can use lock correct ? should i pass the part of the multithread tell be advanced or i should learn it , i have leaned the basics of thread and threading and i think that's enough for my level correct and then i will learn about tasks
Jimmacle
Jimmacle4mo ago
maybe? locks are only one of several synchronization options if i were you i wouldn't spend so much time on contrived examples, work on real projects and learn C# as needed to complete those projects i haven't needed to use the thread APIs in ages
steven preadly
steven preadlyOP4mo ago
I learned c# for making web applications and api so I think it's enough what I learned in c# and go to learn asp.net core
Jimmacle
Jimmacle4mo ago
do you have full completed projects that make use of those skills?
steven preadly
steven preadlyOP4mo ago
No I just want to learn c# basics and then learn asp.net core And make a web applications
Jimmacle
Jimmacle4mo ago
i can tell you right now that if your goal is to learn ASP.NET Core then there is very little point learning threading this way right now because you don't do threading like this in ASP.NET Core it's much more heavily async/task based
steven preadly
steven preadlyOP4mo ago
Then I want to learn async and await and then go to asp.net core correct
Jimmacle
Jimmacle4mo ago
if i were you i would go straight to some ASP.NET Core tutorials, and if you find something you don't understand then pause and learn that learning a bunch of concepts without actually applying them to something isn't a particularly effective way to learn
steven preadly
steven preadlyOP4mo ago
Agree
steven preadly
steven preadlyOP4mo ago
This the link that contains all of the things that I have learned about c#https://prickly-collard-820.notion.site/C-Tutorial-b520663dc5884d8e8ba8cbe066cd0a67
mina remon's Notion on Notion
C# Tutorial | Notion
1-Main Method ():
steven preadly
steven preadlyOP4mo ago
It is written by me Is that enough for asp.net core
Jimmacle
Jimmacle4mo ago
it's probably close enough you won't know until you actually try
steven preadly
steven preadlyOP4mo ago
I will start learning SQL server and asp net core
Jimmacle
Jimmacle4mo ago
i suggest picking a project to make with ASP.NET Core and then figure out what you need to learn to make it work
steven preadly
steven preadlyOP4mo ago
I don't even have the basics of asp net core I have a course that will teach me the basics with a project But do I need to learn SQL server t-sql I already know how to write SQL code because I know Laravel and I am using MySQL
Jimmacle
Jimmacle4mo ago
what i'm saying is, just start a project fill in the gaps as you go instead of trying to predict what you need to know first
steven preadly
steven preadlyOP4mo ago
Hem got the point I will @Jimmacle thank you
Want results from more Discord servers?
Add your server