Posts

STL Questions

1. Sorting a vector of custom objects     a)Using operator()         struct MyStruct {             int key;             std::string str;             MyStruct(int k, int s) : key(k), str(s) {}         };         struct less_than{             inline bool operator()(const MyStruct &s1, const MyStruct &b)             {                 return s1.key<s2.key;             }         };         std::vector<MyStruct> vec;         vec.push_back(MyStruct(4, "a")); ...

Producer Consumer problem using mutex

  #include<stdio.h> #include<pthread.h> #include<stdlib.h>   // Max buffer size user for producing #define buff_sz 10   // buffer index would be the current index volatile int buff_idx = -1; // buffer used to produce item volatile char *buff = NULL;;   // Initialization and declaration of muetxt and condition variables pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t buff_not_full = PTHREAD_COND_INITIALIZER; pthread_cond_t buff_not_empty = PTHREAD_COND_INITIALIZER;   // Consumer thread void *Consumer() {          while (1)          {                  // Taking lock to avoid race condition                  pthread_mutex_ lock(&m...