LinuxQuestions.org
Help answer threads with 0 replies.
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
 
Search this Thread
Old 09-07-2003, 02:31 PM   #1
marek
Member
 
Registered: Aug 2003
Location: Poland
Distribution: Slackware 9.1
Posts: 34

Rep: Reputation: 15
send parameter from thread to thread


As is written in this title, i would like to send data from one thread to second by the parameter of first thread. So it should looks like this:

void * my_thread1(void *my_parameter)
{
int statement = 1;
return (void *)statement
}

main (..)
{
int local_value;
pthread_create(thread_ID,..., my_thread1,....);
//I wait as thread my_thread1 will be finished
pthread_join(&thread_ID, (void *)&local_value);
printf("That's data received from my_thread1 = %d, local_value)
}


This is a part of my program which i am interested a lot, could you help me?? (where is the error and what about sending data like float char and double????)
 
Old 09-07-2003, 05:41 PM   #2
eric.r.turner
Member
 
Registered: Aug 2003
Location: Planet Earth
Distribution: Linux Mint Debian Edition (LMDE)
Posts: 215

Rep: Reputation: 31
The problem is how you are returning your value. pthread_join really wants the address of a pointer. Allocate the memory in your thread function, then return the address of that memory. Here's a similar example that works:

Code:
#include <stdio.h>
#include <stdlib.h>    /* for abort    */
#include <string.h>    /* for strerror */
#include <pthread.h>

void* threadFunction(void* arg)
{
   int* statement;
   statement = calloc(1, sizeof(int));
   *statement = 1;
   return(statement);
}

int main( int argc, char* argv[] )
{
   pthread_t threadId;
   int*      threadResult;
   int       threadStatus;

   threadStatus = pthread_create(&threadId,
                                 NULL,
                                 threadFunction,
                                 NULL);

   if(threadStatus != 0) {
      fprintf(stderr,
              "pthread_create failed: %s\n", 
               strerror(threadStatus));
      abort();
   }

   threadStatus = pthread_join(threadId,
                               (void**)&threadResult);

   if(threadStatus != 0) {
      fprintf(stderr,
              "pthread_join failed: %s\n", 
               strerror(threadStatus));
      abort();
   }

   printf("Data received from threadFunction: %d\n", *threadResult);

   free(threadResult);

   return(0);
}
To compile it, use:

Code:
gcc -Wall -o testthread testthread.c -lpthread

Last edited by eric.r.turner; 09-07-2003 at 05:46 PM.
 
Old 09-08-2003, 05:33 AM   #3
marek
Member
 
Registered: Aug 2003
Location: Poland
Distribution: Slackware 9.1
Posts: 34

Original Poster
Rep: Reputation: 15
So your program works perfectly (but i used little different statement in thread fuction ThreadFunction ->
return *statement instead return(statement)

What about my program it also work great but i forgot to delete one line pthread_exit which i had in my thread function (God punish us for stupidity), without this line it works like it should, sending data from one thread to second

Thanks eric for help
 
  


Reply


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to send a signal to a thread stephenwalter Programming 2 10-17-2005 01:07 AM
how to change and pass thread parameter back to main dragondad Programming 3 09-19-2005 01:58 PM
[thread control suggestion] add a "solved" button that the thread starter can click atom LQ Suggestions & Feedback 3 03-24-2005 11:55 AM
Main thread sending notification to child thread rajesh_b Programming 1 09-22-2004 09:15 AM
configure qt thread issue (just compiled qt w/ -thread option) cleff Linux - Software 8 05-07-2004 11:11 PM


All times are GMT -5. The time now is 07:31 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