1

How can I call a function in a separate thread using pthreads?

In Java, the answer is as set out here: How to call a method with a separate thread in Java?

How do I do this in C?

Community
  • 1
  • 1
Rebecca
  • 21
  • 1
  • 1
  • 3
  • 2
    Surely you could search "pthread tutorial/example in C" ? – P.P Apr 17 '15 at 21:55
  • and it returns nothing about this! – Rebecca Apr 17 '15 at 21:56
  • that's funny, one of the top google answers for "pthread tutorial c" http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html – dbarnes Apr 17 '15 at 21:57
  • [It returns a lot.](http://goo.gl/XVgJyj) – P.P Apr 17 '15 at 22:01
  • In the example that is posted, the main function calls function a() by passing one parameter.(This is just calling the function by specifying it in the create method) I need to call function b() from a() by passing multiple parameters. How do i go about this? – Rebecca Apr 17 '15 at 22:07
  • 1
    Simple: `void a(){b(multi, ple, para, meters);}`. What is the relation to POSIX threads? – Ulrich Eckhardt Apr 17 '15 at 22:10
  • Yes, it returns a lot about pthreads! but nothing specific to my question. @BlueMoon – Rebecca Apr 17 '15 at 22:11
  • @UlrichEckhardt Calling them in this way is not thread-specific. – Rebecca Apr 17 '15 at 22:16
  • You are getting all these answers because your question is unclear. You are asking this with a C tag. Don't assume people know java. Explain more clearly what the java does and someone will tell you the equivalent in C. – kaylum Apr 17 '15 at 22:26
  • I'm reasonably familiar with Java and have not seen that particular paradigm of thread calling. More often I've seen things within the creation of a new thread. Could you be a little more clear as to what you are trying to do? – abligh Apr 17 '15 at 22:46
  • @abligh something like this http://stackoverflow.com/questions/3489543/how-to-call-a-method-with-a-separate-thread-in-java – Rebecca Apr 17 '15 at 22:56
  • The example here: https://computing.llnl.gov/tutorials/pthreads/#CreatingThreads does exactly what you want. – abligh Apr 18 '15 at 08:07
  • You can simple call a function from pthread's function – Keivan Aug 21 '18 at 06:40

2 Answers2

2

You should first create a function which accepts a void* as an argument and returns a void*. Then make a variable to hold the thread. After that initialize it, and wait to finish work.

Here is a simple code.

#include<stdio.h>
#include<pthread.h>

void* thread_func(void* argument) {
  printf("My first thread!!!\n");
  printf("Passes argument: %d\n", *(int*)argument);
  pthread_exit(NULL); // you could also return NULL here to exit no difference
}

int main() {
  pthread_t my_thread;
  int a = 10;
  pthread_create(&my_thread, NULL, thread_func, &a); // no parentheses here 
  pthread_join(my_thread, NULL);

  return 0;
}

Just be careful with passing pointers, because it can lead to a lot of problems.

If you have more questions please ask.

P.S. I have found this tutorial for multithreading. The only thing that won't compile in c code is the output because the tutorial is written in c++ and uses the iostream library to output.ALL the thread creations, passing arguments, and so on are fully valid in c code. http://www.tutorialspoint.com/cplusplus/cpp_multithreading.htm

abligh
  • 24,573
  • 4
  • 47
  • 84
anakin
  • 551
  • 4
  • 12
  • Her question seems to be specifically about how to pass parameters, so you may want to expound on that point explicitly. – dho Apr 17 '15 at 22:17
  • how do i call another thread specific function from thread_func()? – Rebecca Apr 17 '15 at 22:20
  • I dont understand what you want to do make another thread from which starts from the same function or ... – anakin Apr 17 '15 at 22:24
  • thread_func() is just a regular C function, you can call your other function from there the same way you'd call anything else. In the same way the sample above calls printf(), for example. – Moldova Apr 18 '15 at 11:26
  • @anakin : you don’t free the thread stack. It means each time you spawn a thread with glibc, 8Mb of memory is leaked. – user2284570 Mar 12 '17 at 19:57
2

Technically, you can't...at least not directly.

A thread is an execution path that the processor is following as it runs your program. In today's environment, there are many instances of multiple threads. End-User application software generally has different threads doing different things. On a server however, different threads are doing the same thing which is serving client requests. In any case, each individual thread is running with its own stack frame and processor state.

If you need to pass data off to a different thread to be processed, then there are two ways to do that:

1) Just create a new thread with the data as an argument.

2) Use a work-queue arrangement.

I would use #2 because then you can have multiple producers and consumers running depending on how the queue is setup.

Here's a couple of examples on how to set this up:

https://code.google.com/p/c-pthread-queue/source/browse/trunk/queue.c

http://williams.comp.ncat.edu/comp450/pthreadPC.c

Here's a really good tutorial on pthreads: https://computing.llnl.gov/tutorials/pthreads/

Hope this helps.

Daniel Rudy
  • 1,411
  • 12
  • 23