Pthread_create problem


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Pthread_create problem
# 1  
Old 02-09-2011
Pthread_create problem

Hi,

I'm trying to do my homework assignment but I am having trouble using the pthread_create fucntion.

Here is my code________________

//Alicia Johnson
//sum_pid program
//creates n number of threads. These threads create a random number
//then adds the number to a global array. Then a for loop iterates through the array
//and adds the numbers together. The sum is outputted

#include<iostream>
#include<pthread.h>
#include<stdlib.h>



void sum_pid(int n){

int i; //counter to print numbers

int total; //counter that holds the sum of all the numbers added together

int sum[n]; //array that holds the random numbers that are genreated


for(i=0; i<n; ++i){
int random=rand() % 10 + 1;
std::cout<<
"This thread has the number "<<random<<std::endl;
sum[i]=random;
if(i==n-1){
for(int j=0; j<n; ++j){
total=total + sum[j];
}
std::cout<<"The sum of the threads is "<<total<<std::endl;
}
}
//term
inate thread
pthread_exit(NULL);



}


int main(int argc, char *argv[]){

int thr_id; //thread id for the newly created thread

int n; //the number of threads to be created

pthread_t p_thread; //threads structure

//Ask the user for number of threads to create
std::cout<<"Enter number: "<<std::endl;
std::cin>>n;


//create a new thread that will execute 'sum_pid()'
thr_id = pthread_create(&p_thread, NULL, (void*)&sum_pid),(void *)&n);

return 0;

}

Here is my Homework Description:

Write a unix pthread program sum_pid that will take
an integer command line argument n, spawn n
threads that will each generate a random number
between -100 and 100, print out their random
number, and then compute and print out the sum of
random numbers.

When I run the program i get these error messages:

Homework3.cpp: In function âint main(int, char**)â:
Homework3.cpp:58: error: invalid conversion from âvoid*â to âvoid* (*)(void*)â
/usr/include/pthread.h:227: error: too few arguments to function âint pthread_create(pthread_t*, const pthread_attr_t*, void* (*)(void*), void*)â
Homework3.cpp:58: error: at this point in file
Homework3.cpp:58: error: expected `;' before â)â token
Homework3.cpp:60: error: expected primary-expression before âreturnâ
Homework3.cpp:60: error: expected `)' before âreturnâ

I have asked my teacher how do I write it in C++ but he only knows how to make it work in C. Please can someone help me.Smilie
# 2  
Old 02-10-2011
Do not post classroom or homework problems in the main forums. Homework and coursework questions can only be posted in this forum under special homework rules.

Please review the rules, which you agreed to when you registered, if you have not already done so.

More-than-likely, posting homework in the main forums has resulting in a forum infraction. If you did not post homework, please explain the company you work for and the nature of the problem you are working on.

If you did post homework in the main forums, please review the guidelines for posting homework and repost.

Thank You.

The UNIX and Linux Forums.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Programming

fork vs pthread_create

Suppose I have a simple program main() with a global varibale int x=0. int x = 0; main() { print("%d\n",x); } I want to create two threads/process which must access this variable x in sync. Which one will be better threads( pthread_create ) or process( fork )? If I go with fork() then... (1 Reply)
Discussion started by: rupeshkp728
1 Replies

2. Programming

pthread_create

The prototype for pthread_create function is like this:- int pthread_create(pthread_t *thread,pthread_attr_t *attr,void *(*start routine),void *arg); Q.1 .Why the return type of the start_routine must be void*?? Q.2. Why should we pass arg by converting into void * only ?? Thank You (3 Replies)
Discussion started by: sunil_abhay
3 Replies

3. Programming

undefined reference to pthread_create

I try to compile a sample c code in fedora eclipse 3.2 as managed makefile using pthread library,it shows some error on pthread functions.Error is of undefined reference to pthread.Anybody guide me to solve this problem. Thanking you (1 Reply)
Discussion started by: sujith4u87
1 Replies

4. Solaris

pthread_create failed upon execution

Im trying to run an application i compiled (iperf) and i get an error telling me that it cant create the pthread. when i ran the ./configuration one of the things it checked was for pthreads which came back ok. Im not really sure where to even start to resolve this. i have been unable to find... (5 Replies)
Discussion started by: jrich523
5 Replies

5. Linux

problem in Passing arguements to pthread_create

Hello every one My question may look very easy, sorry for my ignorance. I am not able to figure the problem. #include<stdio.h> #include<stdlib.h> #include<pthread.h> void *myfunc(void *vptr_value) { int value = *((int *)vptr_value); printf("Thread value: %d \n", value); ... (1 Reply)
Discussion started by: praveenpvs
1 Replies

6. Programming

How Can I use pthread_create ?

Hi. I use C++ and I wishes to create a thread with the pthread_create function, my question is, how can I do this if I wish that the function will be a member of the class ?? I know from windows programming that I can declare a static function like this static unsigned int __stdcall... (7 Replies)
Discussion started by: shvalb
7 Replies

7. Programming

Pthread_create issue

Hello My problem goes like this: I have used Pthread_create, and I have tryed to create 2 proccess but nothing happens! It does not even matter what the function im trying to create do. It is if im trying to activate an empty function. This is my code. Any help will be highly appreciated.... (1 Reply)
Discussion started by: Hellboy
1 Replies

8. Programming

unresolve pthread_create etc

how to do with that? after cc -o xxxx xxxx.c ld: Unresolved: _pthread_create _pthread_deteach _pthread_exit Thanks (3 Replies)
Discussion started by: zhshqzyc
3 Replies

9. Programming

pthread_create problem

Here is simple code for multithreading in POSIX: void* simplethread(void* arg) { printf("Hello World\n"); } int main(void) { pthread_t id; pthread_create(&id, NULL, simplethread, NULL); return 0; } Whether the new thread will run or not depends on the OS. Tricky ... (5 Replies)
Discussion started by: _rocky
5 Replies
Login or Register to Ask a Question