LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 02-11-2011, 11:43 AM   #1
HarryBoy
Member
 
Registered: Apr 2008
Distribution: MontaVista Linux Version 4.0.1, Professional Edition
Posts: 215

Rep: Reputation: 16
passing data to a new thread


Hi,

I have to launch a new thread from my existing main program and pass data to it.
For now the data is simply a string value.

How I go about passing data to a new thread while it is running.

thanks
 
Old 02-11-2011, 01:42 PM   #2
corp769
Guru
 
Registered: Apr 2005
Posts: 5,798

Rep: Reputation: 978Reputation: 978Reputation: 978Reputation: 978Reputation: 978Reputation: 978Reputation: 978Reputation: 978
It would help if you gave us more information, like what language you are using, sample code you are using, etc...
 
Old 02-11-2011, 04:37 PM   #3
HarryBoy
Member
 
Registered: Apr 2008
Distribution: MontaVista Linux Version 4.0.1, Professional Edition
Posts: 215

Original Poster
Rep: Reputation: 16
Sorry I'm using C++.

I will have a simple main program, will launch a new thread and wish to pass data to it.

Thanks
 
Old 02-11-2011, 04:59 PM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
If you're using pthread_create, pass a pointer as the last argument.
Code:
#include <pthread.h>
#include <string>
#include <iostream>


typedef std::string thread_data;

void *thread(thread_data *dData)
{
	if (dData) std::cout << "DATA: '" << *dData << "'\n";
	delete dData;
}


int main()
{
	pthread_t thread_handle;
	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)
	{
	delete new_data;
	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* for pthread_create.)

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.
Kevin Barry

Last edited by ta0kira; 02-11-2011 at 05:08 PM.
 
Old 02-11-2011, 05:04 PM   #5
HarryBoy
Member
 
Registered: Apr 2008
Distribution: MontaVista Linux Version 4.0.1, Professional Edition
Posts: 215

Original Poster
Rep: Reputation: 16
Thanks for the quick reply.

My main program will run for a while and at various times its will have more data that it needs to pass to the thread.

Is it possible to pass more data to the thread after it has started up?
 
Old 02-11-2011, 05:14 PM   #6
HarryBoy
Member
 
Registered: Apr 2008
Distribution: MontaVista Linux Version 4.0.1, Professional Edition
Posts: 215

Original Poster
Rep: Reputation: 16
sorry I missed this:
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.

It will be string data, it won't be passed very often. A few times every minute or so.

Thanks
 
  


Reply

Tags
c++, threads


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Passing an object to a running thread TheIndependentAquarius Programming 4 01-19-2011 02:28 AM
[SOLVED] Passing array to thread function zak100 Programming 4 08-12-2010 11:07 AM
Problem with passing a class address to a thread nalsrayatko Programming 2 11-25-2007 11:59 AM
passing int array to thread? Thinking Programming 2 09-21-2005 11:00 AM
Passing Arguments into the Thread Function George_gk Programming 2 01-31-2005 05:03 AM


All times are GMT -5. The time now is 08:28 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration