Skip to content

Instantly share code, notes, and snippets.

@calebreister
Last active August 29, 2015 14:16
Show Gist options
  • Save calebreister/9a8d7ddbf5b45cd18d9d to your computer and use it in GitHub Desktop.
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.
//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