Skip to content

Commit d10706e

Browse files
author
Solarian
committed
'second version'
1 parent 0f61bed commit d10706e

9 files changed

+468
-451
lines changed

cpp_thread_00.cpp

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
//Create a C++11 thread from the main program
22

3-
43
#include <iostream>
54
#include <thread>
65

7-
using namespace std;
8-
96
//This function will be called from a thread
10-
void call_from_thread(){
11-
cout<<"Launched by thread"<<endl;
7+
8+
void call_from_thread() {
9+
std::cout << "Hello, World!" << std::endl;
1210
}
1311

12+
int main() {
13+
//Launch a thread
14+
std::thread t1(call_from_thread);
1415

15-
int main(){
16-
//Launch a thread
17-
thread t1 = thread(call_from_thread);
18-
19-
//Join the thread with the main thread
20-
t1.join();
21-
return(0);
16+
//Join the thread with the main thread
17+
t1.join();
18+
19+
return 0;
2220
}

cpp_thread_01.cpp

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,28 @@
33
#include <iostream>
44
#include <thread>
55

6-
using namespace std;
7-
8-
#define NUM_THREADS 10
6+
static const int num_threads = 10;
97

108
//This function will be called from a thread
11-
void call_from_thread(){
12-
cout<<"Launched by thread\n";
9+
10+
void call_from_thread() {
11+
std::cout << "Launched by thread\n";
1312
}
1413

15-
int main(){
16-
thread *t = new thread[NUM_THREADS];
17-
18-
//Launch a group of threads
19-
for(int i=0;i<NUM_THREADS;i++){
20-
t[i] = thread(call_from_thread);
21-
}
22-
23-
cout<<"Launched from the main\n";
24-
25-
//Join the threads with the main thread
26-
for(int i=0;i<NUM_THREADS;i++){
27-
t[i].join();
28-
}
29-
30-
delete [] t;
31-
return(0);
14+
int main() {
15+
std::thread t[num_threads];
16+
17+
//Launch a group of threads
18+
for (int i = 0; i < num_threads; ++i) {
19+
t[i] = std::thread(call_from_thread);
20+
}
21+
22+
std::cout << "Launched from the main\n";
23+
24+
//Join the threads with the main thread
25+
for (int i = 0; i < num_threads; ++i) {
26+
t[i].join();
27+
}
28+
29+
return 0;
3230
}

cpp_thread_02.cpp

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,28 @@
33
#include <iostream>
44
#include <thread>
55

6-
using namespace std;
7-
8-
#define NUM_THREADS 10
6+
static const int num_threads = 10;
97

108
//This function will be called from a thread
11-
void call_from_thread(int tid){
12-
cout<<"Launched by thread "<<tid<<endl;
9+
10+
void call_from_thread(int tid) {
11+
std::cout << "Launched by thread " << tid << std::endl;
1312
}
1413

15-
int main(){
16-
thread *t = new thread[NUM_THREADS];
17-
18-
//Launch a group of threads
19-
for(int i=0;i<NUM_THREADS;i++){
20-
t[i] = thread(call_from_thread,i);
21-
}
22-
23-
cout<<"Launched from the main\n";
24-
25-
//Join the threads with the main thread
26-
for(int i=0;i<NUM_THREADS;i++){
27-
t[i].join();
28-
}
29-
30-
delete [] t;
31-
return(0);
14+
int main() {
15+
std::thread t[num_threads];
16+
17+
//Launch a group of threads
18+
for (int i = 0; i < num_threads; ++i) {
19+
t[i] = std::thread(call_from_thread, i);
20+
}
21+
22+
std::cout << "Launched from the main\n";
23+
24+
//Join the threads with the main thread
25+
for (int i = 0; i < num_threads; ++i) {
26+
t[i].join();
27+
}
28+
29+
return 0;
3230
}

image_processing/ppm.cpp

Lines changed: 118 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -7,138 +7,140 @@
77
#include "ppm.h"
88

99
//init with default values
10-
void ppm::init(){
11-
flag_alloc = false;
12-
width=0;
13-
height=0;
14-
max_col_val=255;
10+
11+
void ppm::init() {
12+
flag_alloc = false;
13+
width = 0;
14+
height = 0;
15+
max_col_val = 255;
1516
}
1617

1718
//create a PPM object
18-
ppm::ppm(){
19-
init();
19+
20+
ppm::ppm() {
21+
init();
2022
}
2123

2224
//create a PPM object and fill it with data stored in fname
23-
ppm::ppm(const std::string &fname){
24-
init();
25-
read(fname);
25+
26+
ppm::ppm(const std::string &fname) {
27+
init();
28+
read(fname);
2629
}
2730

2831
//create an "epmty" PPM image with a given width and height;the R,G,B arrays are filled with zeros
29-
ppm::ppm(const unsigned int _width, const unsigned int _height){
30-
init();
31-
width = _width;
32-
height = _height;
33-
nr_lines = height;
34-
nr_columns = width;
35-
size = width*height;
36-
37-
r = new unsigned char[size];
38-
g = new unsigned char[size];
39-
b = new unsigned char[size];
40-
flag_alloc = true;
41-
42-
for(unsigned int i = 0; i < size; i++){
43-
r[i] = 0;
44-
g[i] = 0;
45-
b[i] = 0;
46-
}
32+
33+
ppm::ppm(const unsigned int _width, const unsigned int _height) {
34+
init();
35+
width = _width;
36+
height = _height;
37+
nr_lines = height;
38+
nr_columns = width;
39+
size = width*height;
40+
41+
r = new unsigned char[size];
42+
g = new unsigned char[size];
43+
b = new unsigned char[size];
44+
flag_alloc = true;
45+
46+
for (unsigned int i = 0; i < size; ++i) {
47+
r[i] = 0;
48+
g[i] = 0;
49+
b[i] = 0;
50+
}
4751
}
4852

4953
//free the memory used by the R,G,B vectors when the object is destroyed
50-
ppm::~ppm(){
51-
if(flag_alloc){
52-
delete [] r;
53-
delete [] g;
54-
delete [] b;
55-
}
54+
55+
ppm::~ppm() {
56+
if (flag_alloc) {
57+
delete [] r;
58+
delete [] g;
59+
delete [] b;
60+
}
5661
}
5762

5863
//read the PPM image from fname
59-
void ppm::read(const std::string &fname){
60-
std::ifstream inp(fname.c_str(), std::ios::in|std::ios::binary);
61-
if(inp.is_open()){
62-
std::string line;
63-
std::getline(inp,line);
64-
if(line != "P6"){
65-
std::cout<<"Error. Unrecognized file format."<<std::endl;
66-
return;
67-
}
68-
std::getline(inp,line);
69-
while(line[0]=='#'){
70-
std::getline(inp,line);
71-
}
72-
std::stringstream dimensions(line);
73-
74-
try{
75-
dimensions>>width;
76-
dimensions>>height;
77-
nr_lines = height;
78-
nr_columns = width;
79-
}
80-
catch(std::exception &e){
81-
std::cout<<"Header file format error. "<<e.what()<<std::endl;
82-
return;
83-
}
84-
85-
std::getline(inp,line);
86-
std::stringstream max_val(line);
87-
try{
88-
max_val>>max_col_val;
89-
}
90-
catch(std::exception &e){
91-
std::cout<<"Header file format error. "<<e.what()<<std::endl;
92-
return;
93-
}
94-
95-
size = width*height;
96-
97-
r = new unsigned char[size];
98-
g = new unsigned char[size];
99-
b = new unsigned char[size];
100-
flag_alloc = true;
101-
102-
char aux;
103-
for(unsigned int i = 0; i < size; i++){
104-
inp.read(&aux,1);
105-
r[i] = (unsigned char) aux;
106-
inp.read(&aux,1);
107-
g[i] = (unsigned char) aux;
108-
inp.read(&aux,1);
109-
b[i] = (unsigned char) aux;
110-
}
111-
}
112-
else{
113-
std::cout<<"Error. Unable to open "<<fname<<std::endl;
114-
}
115-
inp.close();
64+
65+
void ppm::read(const std::string &fname) {
66+
std::ifstream inp(fname.c_str(), std::ios::in | std::ios::binary);
67+
if (inp.is_open()) {
68+
std::string line;
69+
std::getline(inp, line);
70+
if (line != "P6") {
71+
std::cout << "Error. Unrecognized file format." << std::endl;
72+
return;
73+
}
74+
std::getline(inp, line);
75+
while (line[0] == '#') {
76+
std::getline(inp, line);
77+
}
78+
std::stringstream dimensions(line);
79+
80+
try {
81+
dimensions >> width;
82+
dimensions >> height;
83+
nr_lines = height;
84+
nr_columns = width;
85+
} catch (std::exception &e) {
86+
std::cout << "Header file format error. " << e.what() << std::endl;
87+
return;
88+
}
89+
90+
std::getline(inp, line);
91+
std::stringstream max_val(line);
92+
try {
93+
max_val >> max_col_val;
94+
} catch (std::exception &e) {
95+
std::cout << "Header file format error. " << e.what() << std::endl;
96+
return;
97+
}
98+
99+
size = width*height;
100+
101+
r = new unsigned char[size];
102+
g = new unsigned char[size];
103+
b = new unsigned char[size];
104+
flag_alloc = true;
105+
106+
char aux;
107+
for (unsigned int i = 0; i < size; ++i) {
108+
inp.read(&aux, 1);
109+
r[i] = (unsigned char) aux;
110+
inp.read(&aux, 1);
111+
g[i] = (unsigned char) aux;
112+
inp.read(&aux, 1);
113+
b[i] = (unsigned char) aux;
114+
}
115+
} else {
116+
std::cout << "Error. Unable to open " << fname << std::endl;
117+
}
118+
inp.close();
116119
}
117120

118121
//write the PPM image in fname
119-
void ppm::write(const std::string &fname){
120-
std::ofstream inp(fname.c_str(), std::ios::out|std::ios::binary);
121-
if(inp.is_open()){
122-
123-
inp<<"P6\n";
124-
inp<<width;
125-
inp<<" ";
126-
inp<<height<<"\n";
127-
inp<<max_col_val<<"\n";
128-
129-
char aux;
130-
for(unsigned int i = 0; i < size; i++){
131-
aux = (char)r[i];
132-
inp.write(&aux,1);
133-
aux = (char)g[i];
134-
inp.write(&aux,1);
135-
aux = (char)b[i];
136-
inp.write(&aux,1);
137-
}
138-
}
139-
else{
140-
std::cout<<"Error. Unable to open "<<fname<<std::endl;
141-
}
142-
inp.close();
143-
}
144122

123+
void ppm::write(const std::string &fname) {
124+
std::ofstream inp(fname.c_str(), std::ios::out | std::ios::binary);
125+
if (inp.is_open()) {
126+
127+
inp << "P6\n";
128+
inp << width;
129+
inp << " ";
130+
inp << height << "\n";
131+
inp << max_col_val << "\n";
132+
133+
char aux;
134+
for (unsigned int i = 0; i < size; ++i) {
135+
aux = (char) r[i];
136+
inp.write(&aux, 1);
137+
aux = (char) g[i];
138+
inp.write(&aux, 1);
139+
aux = (char) b[i];
140+
inp.write(&aux, 1);
141+
}
142+
} else {
143+
std::cout << "Error. Unable to open " << fname << std::endl;
144+
}
145+
inp.close();
146+
}

0 commit comments

Comments
 (0)