Junior — Middle
What methods are available in standard libraries for creating streams?
sobes.tech AI
Answer from AI
In standard libraries of C and C++, there are several ways to create threads:
- C (POSIX Threads, pthreads): In POSIX systems (Linux, macOS), the pthread.h library is used for working with threads.
#include <pthread.h>
#include <stdio.h>
void* thread_func(void* arg) {
printf("Hello from thread!\n");
return NULL;
}
int main() {
pthread_t thread;
pthread_create(&thread, NULL, thread_func, NULL);
pthread_join(thread, NULL);
return 0;
}
- C++11 and later (std::thread): The C++11 standard introduced a thread library that simplifies thread creation and management.
#include <thread>
#include <iostream>
void thread_func() {
std::cout << "Hello from thread!" << std::endl;
}
int main() {
std::thread t(thread_func);
t.join();
return 0;
}
Thus, in C++, it is preferable to use std::thread, while in C, pthreads are used.