同时运行两个函数?

时间:2021-12-28 20:41:51

I have two functions in C:

我在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!

我怎么能同时运行这两个呢?如果可能,请提供一个例子!

3 个解决方案

#1


8  

You would use threads.

您将使用线程。

For example, pthreads is a c library for multithreading.

例如,pthreads是用于多线程的c库。

You can look at this pthreads tutorial for more details.

您可以在pthreads教程中了解更多细节。

Here's an example of a program spawning pthreads from this tutorial.

下面是本教程中生成pthreads程序的示例。

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

除了(你可能知道)“完全相同的时间”在技术上是不可能的。无论您是在一个单核或多核进程上运行,您都要听从操作系统的调度程序来运行您的线程。不能保证他们会同时运行,相反,他们可以共享一个核心。您可以启动两个线程并并发运行它们,但这可能不是您想要的……

#2


3  

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.

用标准的方法做是不可能的。您需要依赖第三方方法。即pthreads或Win32线程。

POSIX example:

POSIX的例子:

#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:

Win32示例:

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

如果您想在两个平台上使用POSIX,则有Windows端口。

#3


1  

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)

对于“同时”的近似,您可以在自己的线程中执行每个函数(更多信息请参见https://computing.llnl.gov/tutorials/pthreads/)

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.

如果您需要确保这两个函数以某种同步方式执行操作,那么您将需要学习同步原语,如互斥锁、信号量和监视器。

#1


8  

You would use threads.

您将使用线程。

For example, pthreads is a c library for multithreading.

例如,pthreads是用于多线程的c库。

You can look at this pthreads tutorial for more details.

您可以在pthreads教程中了解更多细节。

Here's an example of a program spawning pthreads from this tutorial.

下面是本教程中生成pthreads程序的示例。

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

除了(你可能知道)“完全相同的时间”在技术上是不可能的。无论您是在一个单核或多核进程上运行,您都要听从操作系统的调度程序来运行您的线程。不能保证他们会同时运行,相反,他们可以共享一个核心。您可以启动两个线程并并发运行它们,但这可能不是您想要的……

#2


3  

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.

用标准的方法做是不可能的。您需要依赖第三方方法。即pthreads或Win32线程。

POSIX example:

POSIX的例子:

#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:

Win32示例:

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

如果您想在两个平台上使用POSIX,则有Windows端口。

#3


1  

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)

对于“同时”的近似,您可以在自己的线程中执行每个函数(更多信息请参见https://computing.llnl.gov/tutorials/pthreads/)

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.

如果您需要确保这两个函数以某种同步方式执行操作,那么您将需要学习同步原语,如互斥锁、信号量和监视器。