Last active
August 29, 2015 14:16
-
-
Save calebreister/9a8d7ddbf5b45cd18d9d to your computer and use it in GitHub Desktop.
Basic multithread testing. We may use something like this to run a camera on our 2015 FRC robot.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//g++ test.cc -std=c++11 -lpthread | |
#include <iostream> | |
#include <thread> | |
#include <chrono> | |
using namespace std; | |
void f() { //function to run in 2nd thread | |
for (int i = 0; i < 10; ++i) | |
{ | |
cout << "Thread 2\n"; | |
this_thread::sleep_for(chrono::seconds(5)); | |
} | |
} | |
int main() { | |
thread t(f); //2nd thread | |
for (int i = 0; i < 10; ++i) | |
{ | |
cout << "Thread 1\n"; | |
this_thread::sleep_for(chrono::seconds(1)); | |
} | |
t.join(); //prevents interrupt when Thread 1 finishes before Thread 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment