If you're using pthread_create
, pass a pointer as the last argument.
typedef std::string thread_data;
void *thread(thread_data *dData)
if (dData) std::cout << "DATA: '" << *dData << "'\n";
thread_data *new_data = new thread_data("this is data for the thread");
if (pthread_create(&thread_handle, NULL, (void*(*)(void*)) &thread,
static_cast <thread_data*> (new_data)) < 0)
std::cerr << "error allocating thread\n";
else pthread_join(thread_handle, NULL);
(NOTE: The static_cast
is necessary when using C++ to make sure the pointer is of the correct type before it's converted to void*
It's a lot more complicated to pass data to the thread after it's started, though. What is the nature of the data-passing, e.g. what type, how often, and how many times? A pipe might be the way to go.