#include <thread>voiddo_something(){// Do something in the new thread}intmain(){std::thread t(do_something);t.join();return0;}
2. Execute a function after a delay:
#include <chrono>#include <thread>voiddo_something(){// Do something after a delay}intmain(){std::this_thread::sleep_for(std::chrono::seconds(5));do_something();return0;}
3. Run a function periodically:
4. Execute a function in a separate process:
5. Execute a system command:
6. Open a file:
7. Write to a file:
8. Read from a socket:
9. Write to a socket:
10. Execute a SQL query:
11. Read from standard input:
12. Write to standard output:
13. Execute a function with arguments:
14. Execute a function with a variable number of arguments:
#include <functional>
void do_something(int a, int b) {
// Do something with a and b
}
int main() {
std::function<void(int, int)> func = do_something;
func(1, 2);
return 0;
}
#include <iostream>
#include <stdarg.h>
void do_something(int n, ...) {
// Do something with n and the variable arguments
va_list args;
va_start(args, n);
for (int i = 0; i < n; i++) {
// Process the next argument
int arg = va_arg