tags:

views:

147

answers:

3

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!

+5  A: 

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...

Stephen
Theres no link to download it?, is it included by default?
Daniel
If you're on linux, yes, dunno about much else. Google has some hits for downloading it.
Stephen
@Daniel: You may already have pthreads available - it would help if you stated what OS and programming environment you are using.
Paul R
It's a linux standard library, although there's a semi-complete [win32 port](http://sourceware.org/pthreads-win32/). Cross-platform threading is notoriously annoying in C, but Boost has a [Thread](http://www.boost.org/doc/libs/1_43_0/doc/html/thread.html) library for C++
Michael Mrozek
Sorry not saying this, i'm using mac os x 10.6
Daniel
"a processor which can only do one thing at a time"... In which century? :-) Multi-cores are now pretty much the norm.
squelart
@Daniel : Yes, mac has pthreads. In a terminal, type `man pthread`.
Stephen
Getting error: ‘functionName’ undeclared (first use in this function)The function exists!
Daniel
@squelart : Yes, I consciously ignored multicore machines... it really only confuses the question - you're still at the mercy of the scheduler. I'll edit to clarify.
Stephen
@Daniel: You're going to have to post your code to work on the error. Also note that threads are fraught with danger, especially if both threads are accessing common data.
Yann Ramin
+1  A: 

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.

James
+2  A: 

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.

GMan