Git Product home page Git Product logo

concurrent-stack's Introduction

Concurrent-Stack

This is a concurrent implementation of the data structure STACK using pthreads in C++.

Objective:

  • To implement a parallel stack wherein multiple threads will be performing different actions on the same stack.
  • To prevent race conditions, deadlocks and other concurrency bugs by using some kind of synchronization mechanism.

Operations defined for this implementation of stack:

  • Insert at the top (Push).
  • Delete at the top (Pop).
  • Search (Peek).
  • Print (Display the stack from top to bottom).

Implementation:

I have implemented a class called STACK with all the operations mentioned above. The stack itself is a vector which is a private member of the class. The class also has another private member named top, which is an integer that holds the index of the topmost element. I have made use of mutexes for synchronization and hence only one thread can access the stack at a time (to prevent the bugs mentioned in the objective).

Files:

  • stack_using_vectors.cpp : This is the main file which has the implementation.
  • Thread_logs.txt : This file will get updated with the logs of creation and joining of threads when the main program is run. (Disabled by default. Please uncomment the required lines to enable logging)
  • input.txt : This file contains a sample input (including all four operations) to the program which can be redirected as command line arguments as discussed in the Compilation and running section.
  • t1.txt : This is an input file containing the edge case where all the threads are trying to delete (pop) from an empty stack.
  • t2.txt : This is an input file containing the edge case where all the threads are trying to search (peek) into an empty stack.
  • t3.txt : This is an input file containing the edge case where all the threads are trying to print an empty stack.
  • t4.txt : This is an input file containing the case where all the threads are trying to insert (push) to a stack.

Compilation and running:

Type: g++ concurrent_stack.cpp -pthread in the terminal to compile the program and get an executable named a.out.

This program takes command line arguments which is of the following format:

  • i followed by the insertion value (without space) for insert (push).
  • d for delete (pop)
  • s for search (peek)
  • p for print (display the stack from top to bottom)

Sample execution command for LINUX: ./a.out i4 i6 i7 s d d d s p i89 d s p

Which means Insert 4, Insert 6, Insert 7, Search, Delete, Delete, Delete, Search, Print, Insert 89, Delete, Search, Print to the program.

The same input can be given from a text file like input.txt by using ./a.out $(cat input.txt)

Note: The input values for pushing (insertion) to the stack are converted to integers using atoi.

concurrent-stack's People

Contributors

dhruval360 avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.