Skip to content

Instantly share code, notes, and snippets.

View eLtronicsVilla's full-sized avatar
💭
love to write code

eLtronicsVilla eLtronicsVilla

💭
love to write code
View GitHub Profile
@eLtronicsVilla
eLtronicsVilla / mutex.cpp
Created July 16, 2019 02:20
Mutex implementation using pthread library in cpp
#include<stdio.h>
#include<string.h>
#include<pthread.h>
#include<stdlib.h>
#include<unistd.h>
pthread_t tid[2];
int counter;
pthread_mutex_t lock;
@eLtronicsVilla
eLtronicsVilla / barriers.py
Created July 16, 2019 02:10
Barriers for synchronization mechanism
from random import randrange
from threading import Barrier, Thread
from time import ctime, sleep
num = 5
# 5 threads will need to pass this barrier to get released.
b = Barrier(num)
names = ["Brijesh", "Shubham", "Anup", "Kamesh"]
def player():
@eLtronicsVilla
eLtronicsVilla / condition.py
Created July 16, 2019 02:03
conditions creation for synchronization mechanism
import random, time
from threading import Condition, Thread
"""
'condition' variable will be used to represent the availability of a produced items.
"""
condition = Condition()
box = []
def producer(box, nitems):
for i in range(nitems):
time.sleep(random.randrange(2, 6)) # Sleeps for some time.
@eLtronicsVilla
eLtronicsVilla / events.py
Created July 16, 2019 01:59
event creation for synchronization mechanism
import random, time
from threading import Event, Thread
event = Event()
def my_wait(event, nloops):
for i in range(nloops):
print("%s. Waiting for the flag to be set." % (i+1))
event.wait() # Blocks until the flag becomes true.
print("Wait complete at:", time.ctime())
@eLtronicsVilla
eLtronicsVilla / semaphore.py
Created July 16, 2019 01:55
Semaphore for synchronization mechanism
import random, time
from threading import BoundedSemaphore, Thread
max_items = 6
"""
Consider 'container' as a container, with a capacity of 6
items. Defaults to 1 item if 'max_items' is passed.
"""
container = BoundedSemaphore(max_items)
def producer(nloops):
for i in range(nloops):
@eLtronicsVilla
eLtronicsVilla / r_lock.py
Created July 16, 2019 01:49
R lock for synchronization mechanism
import threading
num = 0
lock = Threading.Lock()
lock.acquire()
num += 1
lock.acquire() # This will block.
num += 2
lock.release()
@eLtronicsVilla
eLtronicsVilla / lock.py
Created July 16, 2019 01:39
Locking mechanism for synchronization of a Thread
from threading import Lock, Thread
lock = Lock()
value = 1
def add_one():
global value
lock.acquire()
value += 1
lock.release()
@eLtronicsVilla
eLtronicsVilla / shared_file.c
Created July 16, 2019 01:13
Shared data between two process
#define buff_max 25
#define mod %
struct item{
// diffrent member of the produced data
// or consumed data
---------
}
# This python code is simple program for multi-threading
import threading
import os
def add(num1,num2):
# find the thread ID
print("Addition is assigned to thread: {}".format(threading.current_thread().name))
print("ID of process running addition: {}".format(os.getpid()))
# This function used for addition of two number
print("addition result = ",num1+num2)
import numpy as np
import cv2
img = cv2.imread('test.jpg')
imgray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(imgray,127,255,0)
im2, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)