I have two functions in C:
void function1(){
// do something
}
void function2(){
// do something while doing that
}
How would I run these two functions at the exact same time?
If possible, please provide an example!
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You would use threads.
For example, pthreads is a c library for multithreading.
You can look at this pthreads tutorial for more details.
Here’s an example of a program spawning pthreads from this tutorial.
Except that (as you probably know) “exact same time” isn’t technically possible. Whether you’re running on a single or multi-core process, you’re at the mercy of the operating system’s scheduler to run your threads. There is no guarantee that they will run “at the same time”, instead they may time share a single core. You can start two threads and run them concurrently, but that may not be exactly what you’re after…