How to call a function using pthread in c?


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

In Java, the answer is as set out here: <a href="https://stackoverflow.com/questions/3489543/how-to-call-a-method-with-a-separate-thread-in-java" rel="nofollow">How to call a method with a separate thread in Java?</a>

How do I do this in C?


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. <a href="http://www.tutorialspoint.com/cplusplus/cpp_multithreading.htm" rel="nofollow">http://www.tutorialspoint.com/cplusplus/cpp_multithreading.htm</a>


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:

<a href="https://code.google.com/p/c-pthread-queue/source/browse/trunk/queue.c" rel="nofollow">https://code.google.com/p/c-pthread-queue/source/browse/trunk/queue.c</a>

<a href="http://williams.comp.ncat.edu/comp450/pthreadPC.c" rel="nofollow">http://williams.comp.ncat.edu/comp450/pthreadPC.c</a>

Here's a really good tutorial on pthreads: <a href="https://computing.llnl.gov/tutorials/pthreads/" rel="nofollow">https://computing.llnl.gov/tutorials/pthreads/</a>

Hope this helps.


  • How to loop-play an audio with pyaudio?
  • signal handler not working
  • instructions after scanf() call get called twice [duplicate]
  • C: How to safely and properly pass several arguments to a pthread? [duplicate]
  • Allocating a Thread's Stack on a specific NUMA memory
  • how to wait for a spawned thread to finish in python?
  • How to compile Boost multithreaded program?
  • Create thread in suspended mode using pthreads
  • pthread_join error code 3
  • Print in single Pthread
  • Using list comprehension
  • Can you alter the output of %caller{0} in logback to mimic log4j %l specifier?
  • Aliasing a URL inside a custom ASP MVC Route
  • Understanding posix barrier mechanism
  • Targetting ios 7 but supporting ios 5+ monotouch
  • How to create a specific URL for a user during registration?
  • UIWebView not calling UIScrollViewDelegate methods
  • Allowing user css
  • Macro to call a function
  • pthread_create memory leak
  • float vs double comparison [duplicate]
  • Anonymous functions and Maps in Scala
  • How to pass multiple parameters to a thread function
  • Regex failing to match number and dash with letter (or space and letter)
  • Finding the number of elements less than or equal to k in a multiset
  • eC (Ecere) how to not worry about private data fields of a class
  • including Python.h in C++ file CDT
  • System call time out?
  • JavaScriptCore External Arrays
  • How do I check if System::Collections:ArrayList is empty / nullptr / null?
  • Creating a C++ function that calls other Lua function
  • How to use function wrapper in mustache.php?
  • How to define and use opencv mat of user type
  • Record samples being played with OpenAL
  • Can I have the cursor start on a particular column by default in jqgrid's edit mode?
  • Statically linking a C++ library to a C# process using CLI or any other way
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Circular dependency while pushing http interceptor
  • Linker errors when using intrinsic function via function pointer
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET