MESSAGE
DATE | 2016-12-19 |
FROM | Ruben Safir
|
SUBJECT | Subject: [Learn] C++ Threading
|
From learn-bounces-at-nylxs.com Mon Dec 19 22:46:35 2016 Return-Path: X-Original-To: archive-at-mrbrklyn.com Delivered-To: archive-at-mrbrklyn.com Received: from www.mrbrklyn.com (www.mrbrklyn.com [96.57.23.82]) by mrbrklyn.com (Postfix) with ESMTP id C3C1816131A; Mon, 19 Dec 2016 22:46:34 -0500 (EST) X-Original-To: learn-at-nylxs.com Delivered-To: learn-at-nylxs.com Received: from [10.0.0.62] (flatbush.mrbrklyn.com [10.0.0.62]) by mrbrklyn.com (Postfix) with ESMTP id 572E216131A; Mon, 19 Dec 2016 17:34:08 -0500 (EST) From: Ruben Safir To: Billy D Message-ID: Date: Mon, 19 Dec 2016 17:34:08 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.5.1 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="------------3BE6C2757BD9BBFD642D0C56" X-Mailman-Approved-At: Mon, 19 Dec 2016 22:46:32 -0500 Subject: [Learn] C++ Threading X-BeenThere: learn-at-nylxs.com X-Mailman-Version: 2.1.17 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: learn-bounces-at-nylxs.com Sender: "Learn"
This is a multi-part message in MIME format. --------------3BE6C2757BD9BBFD642D0C56 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit
Why is this segfaulting with detach but not join
I've tried to localize all the data.
-- So many immigrant groups have swept through our town that Brooklyn, like Atlantis, reaches mythological proportions in the mind of the world - RI Safir 1998 http://www.mrbrklyn.com
DRM is THEFT - We are the STAKEHOLDERS - RI Safir 2002 http://www.nylxs.com - Leadership Development in Free Software http://www2.mrbrklyn.com/resources - Unpublished Archive http://www.coinhangout.com - coins! http://www.brooklyn-living.com
Being so tracked is for FARM ANIMALS and and extermination camps, but incompatible with living as a free human being. -RI Safir 2013
--------------3BE6C2757BD9BBFD642D0C56 Content-Type: text/plain; charset=UTF-8; name="makefile" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="makefile"
Q1hYOj1nKysgCkNYWEZMQUdTOj0tV2FsbCAtZ2dkYiAgLXBnIC1wdGhyZWFkCgpMREZMQUdT Oj0tTC91c3IvbG9jYWwvbGliL215c3FsIC1sbXlzcWxwcCAtbG15c3FsY2xpZW50IC1segoK Cm15cG5nIDoJcG5nX3Byb2oubyBtYWluLm8KCSR7Q1hYfSAgJHtDWFhGTEFHU30gLW8gbXlw bmcgIHBuZ19wcm9qLm8gbWFpbi5vCgptYWluLm8gOgltYWluX3BuZy5jcHAKCSR7Q1hYfSAk e0NYWEZMQUdTfSAgLW8gbWFpbi5vIC1jIG1haW5fcG5nLmNwcAoKcG5nX3Byb2oubyA6CXBu Z19wcm9qLmNwcAoJJHtDWFh9ICAke0NYWEZMQUdTfSAtbyBwbmdfcHJvai5vIC1jIHBuZ19w cm9qLmNwcAoKY2xlYW4JOiAKCXJtIHBuZ19wcm9qICoubyBtYWtlLmRlcHMKCXRvdWNoICou Y3BwICouaAoKaW5jbHVkZSBtYWtlLmRlcHMKbWFrZS5kZXBzOiAqLmNwcCA7IGdjYyAtTSAq LmNwcCA+JEAK --------------3BE6C2757BD9BBFD642D0C56 Content-Type: text/x-c++src; name="main_png.cpp" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="main_png.cpp"
/* * ===================================================================================== * * Filename: main_png.cpp * * Description: PNG Project - main * * Version: 1.0 * Created: 11/30/2016 02:57:46 PM * Revision: 1.0 * Compiler: gcc * * Author: Ruben Safir (mn), ruben-at-mrbrklyn.com * Company: NYLXS Inc * * ===================================================================================== */ #include #include "png_proj.h"
int main(int argc, char **argv) { // const char * image_file_path = nullptr; const char * image_file_path = "/home/ruben/images/tzfat/tzfat_dawn_blended_fused.png"; //const char * image_file_path = "/home/ruben/photo_album/images/135-red_sea_1.png"; png_proj::Image pngtestfile{image_file_path}; // pngtestfile.read_png(); std::cout << "MAIN: line:" << __LINE__ << " char * image size==>" << sizeof(image_file_path) << std::endl; pngtestfile.read_signature(); pngtestfile.read_chunk(); // while(pngtestfile.getNext() < pngtestfile.get_end() ){ // std::cout << "Hey I am not Kosher Bird, yoy can't eat me!!" << std::endl; // }
return EXIT_SUCCESS; }
--------------3BE6C2757BD9BBFD642D0C56 Content-Type: text/x-chdr; name="png_proj.h" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="png_proj.h"
/* * ===================================================================================== * * Filename: png_proj.h * * Description: png_proj.h * * Version: 1.0 * Created: 11/30/2016 03:50:13 PM * Revision: none * Compiler: gcc * * Author: Ruben Safir (mn), ruben-at-mrbrklyn.com * Company: NYLXS Inc * * ===================================================================================== */
#ifndef PNGPRJ #define PNGPRJ #include #include #include
namespace png_proj{ std::mutex medco; std::mutex master;
class thread_guard { public: explicit thread_guard(std::thread& mythread): thd{mythread} {}; ~thread_guard() { if(thd.joinable()) thd.join(); }; thread_guard(thread_guard const & )=delete; //prevent copy const thread_guard& operator=(thread_guard const &)=delete; std::thread& thd; };
typedef uint32_t CHUNK; struct IHDR { int32_t width; int32_t height; int8_t depth; int8_t color_type; //0 greyscale, 2 RGB, 3 Palette, 4 greyscale with alpha, 6 RGB with alpha int8_t compress; //must be 0 int8_t filter; // must be zero int8_t interlace; };
/* * ===================================================================================== * Class: CHUNKY * Description: threaded chunk reader * ===================================================================================== */ class CHUNKY { public:
/* ==================== LIFECYCLE ======================================= */ CHUNKY (unsigned char *); /* constructor */ CHUNKY ( const CHUNKY &other ); /* copy constructor */ ~CHUNKY (); /* destructor */
/* ==================== ACCESSORS ======================================= */ unsigned char *& index(){ return index_; }; unsigned char * &index(unsigned char * pic_position){ index_ = pic_position; return index_; }; std::string &type(){ return type_; }; uint32_t &length() { return length_; }; uint32_t &length(uint32_t value){ return length_ = value; }; unsigned char * & data(){ return data_; }; unsigned char * &data(unsigned char * value){ // std::lock_guard turn(master); // std::cout << "Master Lock Set" << std::endl;
if(value == nullptr){ std::cerr << "assigning empty chunk" << std::endl; exit(1); } std::cout << std::endl << "LINE: " << __FILE__ << " " << __func__ << " " << __LINE__ << std::endl; std::cout << length() << " length" << std::endl; if(length() == 0) //cant seem to create an array with zero elements return data_ = new unsigned char[1]{0};
data_ = new unsigned char[length()]{0}; std::cout << std::endl << "LINE: " << __FILE__ << " " << __func__ << " " << __LINE__ << std::endl; std::cout << "Loading Payload of a Data Slice from Original Image Data" < for(unsigned int i = 0; i < length(); i++ ){ data_[i] = value[i]; } std::cout << std::endl << "LINE: " << __FILE__ << " " << __func__ << " " << __LINE__ ; std::cout << std::endl << "DATA's Address: " << reinterpret_cast(data_) << std::endl; return data_; }; uint32_t &cr() { return crc_; }; uint32_t &cr(uint32_t value) { return crc_ = value; }; unsigned char *begin() { return chunk_canvas_index; };
void begin(unsigned char * value ) { chunk_canvas_index = value; }; unsigned char *end() { return next_canvas_index; };
void end(unsigned char * value ) { next_canvas_index = value; };
/* ==================== MUTATORS ======================================= */ void brush_stroke();
/* ==================== OPERATORS ======================================= */
const CHUNKY& operator = ( const CHUNKY &other ); /* assignment operator */
unsigned char & operator[](unsigned int value){ if(value >= length()){ std::cerr << "out of range" << std::endl; return data_[0]; } return data_[value]; };
protected:
private: /*=================== Private Accessory =====================================*/ std::string &type(std::string value){ type_ = value; return type_; }; std::string &type(char * type_name){ type_.resize(4); type_.assign(type_name, 4); return type_; };
/* ==================== DATA MEMBERS ======================================= */
unsigned char * index_; std::string type_ ; unsigned char t_[5]; uint32_t length_; unsigned char * data_; //payload uint32_t crc_; unsigned char * t(){ return t_; //4 byte type }; unsigned char * t(unsigned char * value){ //this is TYPE rawdata t_[1] = value[1]; t_[2] = value[2]; t_[3] = value[3]; t_[4] = '\0'; t_[0] = value[0];
return t_; };
unsigned char * chunk_canvas_index; unsigned char * next_canvas_index;
}; /* ----- end of class CHUNKY ----- */
/* * ===================================================================================== * Class: Image * Description: Basic Image Library for PNG and Parrallel Programming Class * ===================================================================================== */ class Image { public: /* ==================== LIFECYCLE ======================================= */ Image (); /* constructor */ Image(const char *);
/* ==================== ACCESSORS ======================================= */
unsigned char * get_index ( ) { return index; }; /* ----- end of method Image::get_index ----- */
inline void set_index ( unsigned char * value ) { index = value; return ; }; /* ----- end of method Image::set_index ----- */
unsigned char * get_pic ( ) { return pic; }; /* ----- end of method Image::get_pic ----- */ unsigned char* get_signature() { return loc_signature_cpy; }; /* ----- end of method Image::get_signature ----- */
void set_signature () { unsigned char * start = loc_signature_cpy; //loc_signature_cpy is an array int he constructor of 8 chars // unsigned char * value = get_pic(); std::cout << "setting the signature array**" << std::endl; std::cout << "**************************" << std::endl; for(int i = 0; i<8; i++){ std::cout << "setting the signature" << *get_index() << std::endl; *start = *get_index(); //this is a copy std::cout << "signature set to =>>" << *start<< std::endl; next_index(); start++; } return ; }; /* ----- end of method Image::set_signature ----- */
void set_pic ( unsigned char * value ) { pic = value; return ; }; /* ----- end of method Image::set_pic ----- */
long unsigned int get_length ( ) { return length; }; /* ----- end of method Image::get_length ----- */
long unsigned set_length ( long unsigned int value ) { length = value; return value ; }; /* ----- end of method Image::set_length ----- */
unsigned char * get_end() { return end; }; /* --- end of method Image::get_end --- */
void set_end(unsigned char * value) { end = value; }; /* --- end of method Image::set_end --- */
unsigned char * canvas_index() { return canvas_index_; }; /* -- end of method Image::canvas_index --- */
void canvas_index(unsigned char * value) { canvas_index_ = value;
};/* -- end of method Image::canvas_index -- */
/* ==================== MUTATORS in CLASS Image:: ======================================= */ void read_png( const unsigned char * in = nullptr ); void read_signature(); void read_chunk(); std::thread getNext( ); CHUNKY * set_chunk() { std::cout << std::endl << "ENTER IMAGE::SET_CHUNK:: LINE: " << __func__ << " " << __LINE__ << std::endl; std::lock_guard turn(master); std::cout << "Master Lock Set" << std::endl; unsigned char *cur = reinterpret_cast( get_index() ); CHUNKY * new_chunk = new CHUNKY(cur); std::cout << std::endl << __FILE__ << " LINE: " << __func__ << " " << __LINE__ << std::endl; set_index(get_index() + 12 + new_chunk->length() ) ; std::cout << "Length after reading new chunk " << new_chunk->length() << std::endl; std::cout << "Leaving set_chunk:: Master Lock Unset" << std::endl; return new_chunk;
};
CHUNKY & set_canvas(CHUNKY &yourit) { std::cout << std::endl << "LINE: " << __FILE__ << " " << __func__ << " " << __LINE__ << std::endl; std::lock_guard turn(medco); std::cout << "Medco Lock Set" << std::endl; yourit.begin( canvas_index() ); yourit.end( canvas_index() + yourit.length() ); canvas_index( yourit.end() ); std::cout << "Lock " << std::dec << std::showbase << reinterpret_cast(yourit.begin() ) << " : " << std::dec << std::showbase << reinterpret_cast( yourit.end() )<< std::endl; return yourit; };
void next_index() { unsigned char * tmp = get_index(); set_index(++tmp); };
void signature() { read_signature(); };
/* ==================== OPERATORS ======================================= */
/* ==================== DATA MEMBERS ======================================= */ protected:
private: unsigned char * pic; unsigned char loc_signature_cpy[8]; unsigned char * index; unsigned char * end; long unsigned int length; //This is the length of the entire image // uint8_t * data = nullptr; // int32_t type; // int32_t crc;
unsigned char * canvas; //this is the outgoign image data unsigned char * canvas_index_; long int canvas_size; long int chunk_dispatch;
int blank_canvas_psize(const IHDR &look ) { int pixil_size; switch (look.color_type) { case 0: if(look.depth == 1) pixil_size = 1; if(look.depth == 2) pixil_size = 1; if(look.depth == 4) pixil_size = 1; if(look.depth == 8) pixil_size = 1; if(look.depth == 16) pixil_size = 2; break; case 2: if(look.depth == 8) pixil_size = 3; if(look.depth == 16) pixil_size = 6; break; case 3: if(look.depth == 1) pixil_size = 3; if(look.depth == 2) pixil_size = 3; if(look.depth == 4) pixil_size = 3; if(look.depth == 8) pixil_size = 3; break; case 4: if(look.depth == 8) pixil_size = 4; if(look.depth == 16) pixil_size = 8; break; case 6: if(look.depth == 8) pixil_size = 4; if(look.depth == 16) pixil_size = 8; break; default: std::cerr << "incorrect color type" << std::endl; exit(EXIT_FAILURE); } return pixil_size; }; }; /* ----- end of class Image ----- */
} #endif
--------------3BE6C2757BD9BBFD642D0C56 Content-Type: text/x-c++src; name="png_proj.cpp" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="png_proj.cpp"
/* * ===================================================================================== * * Filename: png_proj.cpp * * Description: PNG Project * * Version: 1.0 * Created: 11/15/2016 12:08:44 PM * Revision: none * Compiler: gcc * * Author: Ruben Safir (mn), ruben-at-mrbrklyn.com * Company: NYLXS Inc * * ===================================================================================== */ #include #include #include #include #include |
|