This file contains hidden or 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
#include <iostream> | |
#include <unistd.h> | |
#include <thread> | |
using namespace std; | |
void do_greeting3(char arg); | |
//arguments:arg is an untyped pointer pointing to a character | |
// returns:a pointer to NULL | |
// side effects:prints a greeting |
This file contains hidden or 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
// extern crate rand; | |
// use nix::unistd::sleep; | |
// use std::process; | |
use rand::Rng; | |
use std::thread; | |
fn main() { | |
// Use lambda when the thread function takes arguments | |
let thr1 = thread::spawn(|| do_greeting2(30)); | |
let thr2 = thread::spawn(|| do_greeting2(50)); |
This file contains hidden or 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
// Compile with -lpthread flag | |
#include <thread> | |
#include <iostream> | |
#include <sys/types.h> | |
#include <unistd.h> | |
using namespace std; | |
/* Thread functions in C++ can take any number of args of any type */ | |
void do_greeting2 (int, string); |
This file contains hidden or 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
#include <pthread.h> | |
#include <errno.h> | |
#include <unistd.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
void* do_greeting2 (void* arg); |
This file contains hidden or 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
// Compile with -lpthread flag | |
#include <iostream> | |
#include <thread> | |
#include <unistd.h> | |
using namespace std; | |
void do_greeting(); | |
int main() { |
This file contains hidden or 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
use nix::unistd::sleep; | |
use std::process; | |
use std::thread; | |
fn main() { | |
println!( | |
"From PID {} thread id {:?}", | |
process::id(), | |
thread::current().id() | |
); |
This file contains hidden or 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
extern crate signal_hook; | |
use std::error::Error; | |
use std::time::Duration; | |
use nix::unistd::{pause, sleep}; | |
use signal_hook::{register, SIGINT}; | |
static mut I_CAN_RUN: bool = true; | |
fn main() -> Result<(), Box<dyn Error>> { |
This file contains hidden or 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
use nix::unistd::{close, dup2, fork, pipe, ForkResult}; | |
use std::io::{self, Read, Write}; | |
use std::process::{exit, id}; | |
use std::str; | |
use std::error::Error; | |
const MAX: usize = 1024; | |
fn main() -> Result<(),Box<dyn Error>> { |
This file contains hidden or 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
// Be sure to link this program with the math library (-lm) | |
#include <stdio.h> | |
#include <math.h> | |
#include <errno.h> | |
int main() { | |
double x, z; | |
printf ("Enter a number: "); | |
scanf ("%lf", &x); |
This file contains hidden or 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
use nix::unistd::fork; | |
fn main() { | |
println!("Before fork()"); | |
fork(); | |
println!("After fork()"); | |
} |