top of page

Week 9 : Multithreaded Lifestyle...

Class 27 :

It as a Sunday, I was a facilitation session. our tutor went through our progress. Our teacher briefly went over how a world bending shader is made. Then he Introduced us to how multithreading works and explained the common use cases its mostly implemented like A* and independent process. So first of all he began by showing us how a thread is made. for that first we need a reference to the System.Threading assembly. Then we make a thread by using new Thread and pass in an action as a parameter. also we can determine weather to make a thread run in background or shall we wait till the thread finish execution.

using System.Threading;

Thread thread = new Thread(new ThreadStart(WorkThreadFunction));
thread.Start();

public void WorkThreadFunction()
{
  try
  {
    // do any background work
  }
  catch (Exception ex)
  {
    // log errors
  }
}

After that we made a use case of what if two threads work simultaneously accessing and also editing the same data. for that he made two threads and made it to write in characters into a string. and once the code was executed the whole string might appeared jumbled and mixed up. He told us that though the thread was created and assigned for execution there is no way to finding which thread will run when and at what order. and this is what result in the corruption of data. To solve this Mutex was introduced which is basically works as a lock which prevents one thread from running a same function or accessing a data if another thread already began the process. Class 28 :

It as a Thursday, Our tutor introduced us to the concept of a job-system. So in brief a job-system is a method which executes a method passed into a collection were the method is run by a vacant thread on a different core other that the core that runs the main thread. In our implementation we made a Dictionary to hold reference to the jobs we create. The dictionary we created was derived from System.Collections.Concurrent there for is called a concurrent dictionary. We used it cause its thread safe. which means two or more threads can edit the data without crashing the application. Then the next step was to make the job base class which holds reference to a function which the thread supposed to run and a callback. Also we use it to indicate the state of the function weather its not done, is doing or is completed. then we made an execution function which goes through the elements(jobs) in the dictionary and runs them if they are is the state of notDone. The main key step here is for execution the function we uses ThreadPool.QueueUserWorkItem which takes the function and make it run using an available/idle thread. We got it running and all was good. I reached home and attempted making one and it was cool.


namespace JobSystem2021 {     

class Program     {          
public static List<Job> jobList = new List<Job>();          public enum JobStatus{
notDone,
isDoing,
done         
}         
public class Job          
{             
public Job(Action<Object> callBack) {                 this.callback = callBack;             
}              
Action<Object> callback;              
public JobStatus jobStatus = JobStatus.notDone;              public Object result;              
public virtual void Run() {                 Thread.Sleep(1000);                 
Console.WriteLine("I am the Devil...");                 callback.Invoke("Action Compleated...");                 jobStatus = JobStatus.done;                 
result = 100;             
}          
}          
public static void ExecuteJobs()         
{             
while (true)              
{                 
for (int i = 0; i < jobList.Count; i++)                 
{                     
if (jobList[i].jobStatus == JobStatus.notDone)                     {                         
Console.WriteLine("Running...");                         
Job selectedJob = jobList[i];                         selectedJob.jobStatus = JobStatus.isDoing;                         ThreadPool.QueueUserWorkItem((x) => selectedJob.Run());                     }                 
}             
}         
}          
static void ActionDoneResponce(Object outObject) {             Console.WriteLine(outObject as string);         
}          
static void Main(string[] args)         
{             
jobList.Add(new Job(ActionDoneResponce));             ExecuteJobs();         
}     
} 
}


Class 29 :

We were at the class in this Saturday. We were discussing about how to get a job in the current state of the game industry. Our tutor advised us to make connections with people up top. as they prefer hiring people in their near circles and only if they are not able to fine one they will look for someone outside their connection. Then we got into an argument about who have it easier these days, Men or Women when it comes to social advantage. we said its women as men these days are cowards and simps who go to far extremes to win the heart of a women when its completely a waste of time an money. Our tutor said otherwise and sided with the argument that women have it harder these days.

Recent Posts

See All
Week 13 : El Fin...

Class 39 : We join in the class on Sunday. There's is nothing much to teach from our tutors side. all of us guys are doing our curricular...

 
 
 
Week 12 : Ending it rightly...

Class 36 : It was a Sunday. a busy morning. I showed up for the class, the tutor asked us let him know if we need any support with the...

 
 
 

Comments


  • LinkedIn
  • YouTube

© 2017 by Alwin Joshy. Proudly created with Wix.com.

bottom of page