Deliver values to Thread function


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Deliver values to Thread function
# 1  
Old 05-08-2013
Deliver values to Thread function

Hello,
I hope you can help me with my problem.
I've written a programm which creates a thread. The main programmm creates 10 int and float values. The fucnction should give the values out.
May big problem ist that the delivery to the function is not working.
Where ist my fault?
How can I make easier?
Thank you for your help.

Code:
void funktion1 (void *arg1)
{
	
	printf("Int-Zahl= %i",arg1);
	printf("[TID:%li,PPID:%i] Threadfunktion1 \n",syscall(SYS_gettid),getppid() );
	pthread_exit(NULL);
}

int main()
{	
	struct Zahlen{int z1;float z2;};

	struct Zahlen Zahl;

	pthread_t ptr1, ptr2, ptr3;
	int res1;
	int res2; 
	int bes1;
	int bes2;


	// reine Threadzeile
	res1 = pthread_create(&ptr1, NULL, (void *)&funktion1,(void *)Zahl.z1);

	//Fehlerabfrage Thread3
		if(res1 != 0)
	{
		perror("Thread creation failed!");
	}
	int i;	
	for(i = 0; i<=10;i++)
	{
		Zahl.z1 = random() % 100; //Int Zahl 1-100 erzeugt
		Zahl.z2 =(float)rand() / RAND_MAX; //Float Zahl 0-1 erteugt
		printf("[TID: %li, PID: %i]int-Wert: %i  float-Wert: %f \n",syscall(SYS_gettid), getpid(), Zahl.z1, Zahl.z2);
	}
	bes1 = pthread_join(&ptr1, NULL);
	
	/*if (bes1  !=0) 
		{ 
			perror("Thread join failed");		
			exit(1); 
		}
	return 0;
}


Last edited by Scott; 05-08-2013 at 11:21 AM.. Reason: Please use code tags
# 2  
Old 05-08-2013
As per your code, main thread send only one int value to the "funktion1" thread.
After sending one int value, the main thread is modifying its value ten times.

If you want to send all 10 int values at a time, modify the struct as follows
Code:
struct Zahlen{int z1[10]; float z2[10];};

Then fill the structure in loop.

Pass the address of the structure variable to the "funktion1" thread.
Code:
res1 = pthread_create(&ptr1, NULL, (void *)&funktion1,(void *)&Zahl);

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

E-mail will not deliver

I am able to send mail to my gmail account and one or two other accounts, but not to my isp e-mail account and another domain I have set up. I am trying to send from VsiFaxServer@ssc.local. And also from root@ssc.local using the mail command at the prompt. When I check the new messages, I found the... (23 Replies)
Discussion started by: smithssc
23 Replies

2. Programming

unable to use a function to crate a joinable thread

In my program, threads may be created when some events trigger. So I can't create threads on initialization. Theremore,I write a createThread() function to create thread. However, it is blocking at first call and don't run anymore? why? #include <pthread.h> #include <stdio.h> #include... (4 Replies)
Discussion started by: sehang
4 Replies

3. Programming

Passing multiple values from a function in C

I know multiple values can be returned from a function in C like this: char **read_file ( char * , unsigned long int * );//this is the function prototypeunsigned long int number_of_words = 0;//variable defined in main() and initialized to 0words_from_dictionary = read_file ( "dictionary.dit" ,... (2 Replies)
Discussion started by: shoaibjameel123
2 Replies

4. Programming

Thread function local variables

As I know threads share the memory. But, what about the local variables in the thread function? if i call multiple threads would they allocate seperate local variables for themselves? like thread_func() { int i, j; string... } Are the above local variables defined for each of... (1 Reply)
Discussion started by: saman_glorious
1 Replies

5. Shell Programming and Scripting

how to capture oracle function returning 2 values in unix

i have an oracle function which returns two values, one is the error message if the function encounters anything and another one which returns a number i need to capture both and pass it on to unix shell script how to do it (2 Replies)
Discussion started by: trichyselva
2 Replies

6. Shell Programming and Scripting

Function returns wrong values - solved

Hi I have a small function which returns a wrong value. The function tries to make a connection to oracle database and tries to get the open_mode of the database in the variable status. However when a database is down the value of the status column is set to READWRITE i am not sure why. I... (0 Replies)
Discussion started by: xiamin
0 Replies

7. Shell Programming and Scripting

passing values to function in Ksh

Hi, I'm trying to work on the script given below #!/bin/ksh -x pfile() { echo "$1" } touch smp19 echo "Hi" > smp19 result=$(pfile $smp19) echo $result As highlighted , when i pass $smp19 as parameter ,it does not display the output.However when i try giving "Hi" instead... (2 Replies)
Discussion started by: Sheema
2 Replies

8. Programming

returning multiple values from a function in C

hi how can I return multiple values from a C function. I tried the following: #include <stdio.h> void foo(int id, char *first_name, char *last_name) { /* this is just an example to illustrate my problem... real code makes use of the "id" parameter. */ first_name = (char... (8 Replies)
Discussion started by: Andrewkl
8 Replies

9. Programming

create a thread from a returning function

hi all, my requirement is to create a thread by calling another function. i.e i dont call pthread_create directly from main, but by calling another function (createThd - below ), from main. Example: void *thread_function(void *arg) { /* thread function */ int i; rc =... (3 Replies)
Discussion started by: wolwy_pete
3 Replies

10. HP-UX

create thread C with JNI function with JAVA

Hello, J create a thread C with a JNI function via JAVA. J have the following message (but not in each time): Someone has an idea ? Thank. Unexpected Signal : 4 occurred at PC=0x78C103E0 Function= Library=(N/A) NOTE: We are unable to locate the function name... (0 Replies)
Discussion started by: AUBERT
0 Replies
Login or Register to Ask a Question