I have no idea why this doesn't work
#include <iostream> #include <pthread.h> using namespace std; void *print_message(){ cout << "Threading\n"; } int main() { pthread_t t1; pthread_create(&t1, NULL, &print_message, NULL); cout << "Hello"; return 0; }
The error:
[Description, Resource, Path, Location, Type] initializing argument 3 of 'int pthread_create(pthread_t*, const pthread_attr_t*, void* (*)(void*), void*)' threading.cpp threading/src line 24 C/C++ Problem
pthreads or POSIX threads are an implementation of the thread API for C/C++. It allows the spawning of new concurrent process flows and the multithreading system, which allows parallel and distributed processing. It does so by dividing the program into subtasks whose execution can be interleaved to run in parallel.
Yes, the pthreads library is still used for threading.
Thread functions in C/C++ In a Unix/Linux operating system, the C/C++ languages provide the POSIX thread(pthread) standard API(Application program Interface) for all thread related functions. It allows us to create multiple threads for concurrent process flow.
POSIX Threads, commonly known as pthreads, is an execution model that exists independently from a language, as well as a parallel execution model. It allows a program to control multiple different flows of work that overlap in time.
You should declare the thread main as:
void* print_message(void*) // takes one parameter, unnamed if you aren't using it
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With