This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import threading | |
num = 0 | |
lock = Threading.Lock() | |
lock.acquire() | |
num += 1 | |
lock.acquire() # This will block. | |
num += 2 | |
lock.release() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from threading import Lock, Thread | |
lock = Lock() | |
value = 1 | |
def add_one(): | |
global value | |
lock.acquire() | |
value += 1 | |
lock.release() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
NewerOlder