Commit 70fd476a authored by Nelson Phillips's avatar Nelson Phillips
Browse files

autosave

parent 1291831e
#include <iostream> #include <iostream>
#include <stdio.h> #include <stdio.h>
#include "threadClass.h" #include "ReadInput.h"
char ReadInput::collectInput (){ char ReadInput::collectInput (){
system("stty raw"); system("stty raw");
inputCh = getchar(); inputCh = getchar();
system("stty cooked"); system("stty cooked");
return inputCh; return inputCh;
} }
#ifndef THREADCLASS_H #ifndef READINPUT_H
#define THREADCLASS_H #define READINPUT_H
#include <iostream> #include <iostream>
......
// thread example #include <iostream>
#include <iostream> // std::cout #include "ReadInput.h"
#include <thread> // std::thread #include "Threads.h"
#include <unistd.h> #include <thread>
#include <stdio.h>
#include "threadFunc.cpp"
using namespace std; using namespace std;
...@@ -17,23 +16,17 @@ int h = (rand() % 14); ...@@ -17,23 +16,17 @@ int h = (rand() % 14);
int o = (rand() % 30); int o = (rand() % 30);
char letter = ' '; char letter = ' ';
ReadInput inputRead; ReadInput inputRead;
char input; char input;
void Sleep(float s) {
int sec = int(s*250000);
usleep(sec);
}
void charCall() { void charCall() {
for (int i = 0; i < 5000; i++){ for (int i = 0; i < 5000; i++){
input = inputRead.collectInput(); input = inputRead.collectInput();
}
} }
}
Thread Sneko;
void snakePrint(){ void snakePrint(){
for (int i = 0; i < 5000; i++){ for (int i = 0; i < 5000; i++){
Sleep(1); Sneko.Sleep(1);
system("stty cooked"); system("stty cooked");
if(input == 'w'|| input == 'W') if(input == 'w'|| input == 'W')
x--; x--;
...@@ -83,11 +76,19 @@ void charCall() { ...@@ -83,11 +76,19 @@ void charCall() {
} }
} }
int main() int main()
{ {
thread first (charCall); // spawn new thread that calls input() thread first (charCall); // spawn new thread that calls input()
thread second (snakePrint); // spawn new thread that calls snakePrint(0) thread second (snakePrint); // spawn new thread that calls snakePrint(0)
cout << "main, input and snakePrint now execute concurrently...\n"; cout << "Press w,a,s,d to begin.";
// synchronize threads: // synchronize threads:
first.join(); // pauses until first finishes first.join(); // pauses until first finishes
...@@ -96,4 +97,5 @@ int main() ...@@ -96,4 +97,5 @@ int main()
} }
//g++ thread.cpp -o main.out -pthread -std=c++11 && ./main.out //g++ *.cpp -o main.out -pthread -std=c++11 && ./main.out
\ No newline at end of file //echo *.cpp
\ No newline at end of file
#include <iostream>
#include <thread>
#include <unistd.h>
#include "Threads.h"
void Thread::Sleep(float s){
int sec = int(s*250000);
usleep(sec);
}
\ No newline at end of file
#ifndef THREADS_H
#define THREADS_H
#include <iostream>
class Thread {
public:
void Sleep(float s);
};
#endif
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment