Hi,
I have two functions in C:
function1(){
// do something
}
function2(){
// do something while doing that
}
How would i run these two at the exact same time? If possible, please provide an example!
Hi,
I have two functions in C:
function1(){
// do something
}
function2(){
// do something while doing that
}
How would i run these two at the exact same time? If possible, please provide an example!
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.
#include <pthread.h>
#include <stdio.h>
#define NUM_THREADS 5
void *PrintHello(void *threadid)
{
long tid;
tid = (long)threadid;
printf("Hello World! It's me, thread #%ld!\n", tid);
pthread_exit(NULL);
}
int main (int argc, char *argv[])
{
pthread_t threads[NUM_THREADS];
int rc;
long t;
for(t=0; t<NUM_THREADS; t++){
printf("In main: creating thread %ld\n", t);
rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);
if (rc){
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
pthread_exit(NULL);
}
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...
For an approximation to 'at the same time', you could execute each function in its own thread (see https://computing.llnl.gov/tutorials/pthreads/ for more info)
If you need to ensure these two functions perform operations in some kind of synchronized fashion, you will want to learn about synchronization primitives such as mutexes, semaphores and monitors.
It's not possible to do in a standard way. You'll need to rely on some third-party method. Namely, pthreads or Win32 threads.
POSIX example:
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
void* print_once(void* pString)
{
printf("%s", (char*)pString);
return 0;
}
void* print_twice(void* pString)
{
printf("%s", (char*)pString);
printf("%s", (char*)pString);
return 0;
}
int main(void)
{
pthread_t thread1, thread2;
// make threads
pthread_create(&thread1, NULL, print_once, "Foo");
pthread_create(&thread2, NULL, print_twice, "Bar");
// wait for them to finish
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
return 0;
}
Win32 example:
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
DWORD print_once(LPVOID pString)
{
printf("%s", (char*)pString);
return 0;
}
DWORD print_twice(LPVOID pString)
{
printf("%s", (char*)pString);
printf("%s", (char*)pString);
return 0;
}
int main(void)
{
HANDLE thread1, thread2;
// make threads
thread1 = CreateThread(NULL, 0, print_once, "Foo", 0, NULL);
thread2 = CreateThread(NULL, 0, print_once, "Bar", 0, NULL);
// wait for them to finish
WaitForSingleObject(thread1, INFINITE);
WaitForSingleObject(thread2, INFINITE);
return 0;
}
There are Windows ports of POSIX if you want to use it on both platforms.