Results 1 to 5 of 5
Hello,
I have a following problem: i want to create 2 threads using pthreads but the function i must to call in pthread_create() accept two parameters and is implemented in ...
- 01-19-2010 #1Just Joined!
- Join Date
- Jan 2010
- Posts
- 1
how to create a thread calling an external function
Hello,
I have a following problem: i want to create 2 threads using pthreads but the function i must to call in pthread_create() accept two parameters and is implemented in a shared library.
The question is how i can call this function in the moment of thread creation.
Example:
Thanks in advance,Code:#include <pthread.h> #include <stdio.h> extern double foo(int, char**); int main(int argc, char** argv){ pthread_t thread1, thread2; int iret1, iret2; iret1 = pthread_create( &thread1, NULL, ?, ?); <- How to complete the arguments list for foo function iret2 = pthread_create( &thread2, NULL,?, ?); <- How to complete the arguments list foo function pthread_join( thread1, NULL); pthread_join( thread2, NULL); printf("Thread 1 returns: %d\n",iret1); printf("Thread 2 returns: %d\n",iret2); exit(0); }
Jose
- 01-19-2010 #2
About passing multi parameters, pthread_create takes
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,void *(*start_routine) (void *), void *arg);
where
allows you can pass a pointer to anything.Code:void *arg
About the shared library...that shouldn't be a problem, just make sure to load the shared library before you call the functions.Make mine Arch Linux
- 01-20-2010 #3
The short answer is that you can't do it directly. pthread_create requires a pointer to a function that accepts only one argument.
Therefore, you have two choices:
- If you have written the function foo(), and it is only used for this purpose, you can rewrite it to accept only one argument. Because this argument is a void *, you can have the argument be a pointer to a struct that contains as much data as you want.
- Otherwise, you can have pthread_create begin by calling a function of your own that calls the foo function. The parameters can be hardcoded into this function, or may be passed in as described above.DISTRO=Arch
Registered Linux User #388732
- 01-26-2010 #4Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
As was pointed out, the start function can only take one argument, the one that is passed to pthread_create(). However, what is usually done is that arg is a pointer to a structure that has the actual arguments you will use, and the start function is simple a pointer to a wrapper function that takes the components contained in the structure arg points to and then calls your real start function with the arguments in place. This way, you can pass any number of arguments you want and pthread_create() is happily oblivious to that.
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 01-26-2010 #5Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
Here is an example based on your code:
Code:#include <pthread.h> #include <stdio.h> extern double foo(int, char**); struct foo_args { int arg1; char** arg2; }; int start_foo( void* arg ) { struct foo_args* fargs = (struct foo_args*)arg; double foo_ret_val = foo( fargs->arg1, fargs->arg2); /* Do something with foo_ret_val here */ return (int)foo_ret_val; } int main(int argc, char** argv) { pthread_t thread1, thread2; int iret1, iret2; struct foo_args fargs1 = {1, argv}; struct foo_args fargs2 = {2, argv}; iret1 = pthread_create( &thread1, NULL, start_foo, (void*)&fargs1); iret2 = pthread_create( &thread2, NULL, start_foo, (void*)&fargs2); pthread_join( thread1, NULL); pthread_join( thread2, NULL); printf("Thread 1 returns: %d\n",iret1); printf("Thread 2 returns: %d\n",iret2); exit(0); }Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!


Reply With Quote