0

I need to find a way to implement multi threading into my program, i would like to implement it in a way that sends lets say 10 characters to each thread and then each thread runs the encoding process, then the same for the decoding process. Ive done some research but i cant get seem a straight answer.

//dual char hamming program

//this program takes 2 chars from a text file, converts them to binary,then
//finds and inserts parody bits in the (1,2,4,8) poistion, taks on an extra
//1 at the beginning then converts the binary string to a number
//to be saved into a txt file for later decryption.
using namespace std;

int DataEncode();
int TypeEncode();
int Decode();


int main() {
fstream Decoded, Encoded;
string command;
cout << "Enter command: (Decode or Encode or Exit): ";
while(cin >> command) {


if (command == "Encode" || command == "encode") {// start of if for(Encode)
cout << "Encode from data or type? (Data or Type): ";
cin >> command;
if (command == "Type" || command == "type") {
TypeEncode();
}
if (command == "Data" || command == "data") {
DataEncode();
}

}               // end of if(Encode)

if (command == "Decode" || command == "decode") 

{                   // start of if for(Decode) 
Decode();


cout << "Done..." << endl << endl;

}           // end of if(Decode)

if (command == "Exit" || command == "exit") {
Encoded.close();
Decoded.close();
return 0;
}

cout << "Enter command: (Decode or Encode or Exit):";
}

Encoded.close();
Decoded.close();
return 0;
}//end of main

Id like to see the program improve total runtime even if its by as little as 2x

Scheff's Cat
  • 19,528
  • 6
  • 28
  • 56
Briley2K
  • 20
  • 6
  • 1
    Gaining speed-up by multi-threading is not that easy. An alogrithm must be scalable. Communication overhead might cause even "negative speed-up". Communication overhead can be caused by the need for sync. due to locking of concurrent memory accesses which might become necessary with MT. Finally, the creation and joining of threads causes a certain performance impact itself. Thus, without exposing the actual algorithm this is hard to answer. However, I once fiddled a bit with this to get experience: [SO: Multi-threading benchmarking issues](https://stackoverflow.com/a/52835213/7478597). – Scheff's Cat Apr 12 '19 at 07:43
  • There's nothing you can speed up here, as you're only showing the user input code. You'd have to provide the `Decode()`/`DataEncode()`/`TypeEncode()` code. And also take into consideration @Scheff's comment - Multithreading/Parallelization is not a trivial task and there's no magic that simply turns your serial program into a faster multithreaded program. – andreee Apr 12 '19 at 07:44
  • They are fairly large here is the link to my repository https://github.com/MrBriley33/Hamming-Code if you would like me to just paste the code let me know – Briley2K Apr 12 '19 at 08:49
  • @Mr.Briley the first thing you should do is to clearly define your input and output. Currently your Encode and decode read files and write on standard output. You should extract the part which is purely computational in a separate function. – UmNyobe Apr 12 '19 at 10:03
  • alright will do, i will post an update once Ive finished, Thank You. – Briley2K Apr 12 '19 at 18:17
  • Any specific reason you use bool arrays instead of just uint16_t and bitwise operators? IDK, maybe compiler optimizes it well, but not necessarily. Also, I/O may be pretty relevant in this case. You seem to make a lot of potentially blocking I/O, possibly calling write on bigger chunks would speed things up. In any case, you need to decouple your function from the output stream in order to parallelize anything. – Frax Apr 12 '19 at 18:46
  • @Frax i used bool just because it was what came to my mind at the time and it worked and it was good on memory from what i can tell, and the current thing im working on right now is pulling in several characters at a time and converting them to their ASCII equal and storing that into an array, then the array gets pushed to a function that encodes the data. im about to push an update to git hub on what Ive done so far. Thanks for all the input guys. – Briley2K Apr 14 '19 at 20:48

0 Answers0