Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple pthread! C++

Tags:

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

like image 986
Angel.King.47 Avatar asked Jul 16 '09 07:07

Angel.King.47


People also ask

What is a pthread in C?

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.

Is pthread still used?

Yes, the pthreads library is still used for threading.

Does C++ use pthread?

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.

Why we use pthreads?

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.


1 Answers

You should declare the thread main as:

void* print_message(void*) // takes one parameter, unnamed if you aren't using it 
like image 134
Sam Harwell Avatar answered Sep 17 '22 22:09

Sam Harwell