【问题标题】:C: Run two functions at the same time?C:同时运行两个函数?
【发布时间】:2010-06-16 05:54:49
【问题描述】:

我在 C 中有两个函数:

void function1(){
    // do something
}

void function2(){
    // do something while doing that
}

如何同时运行这两个函数? 如果可能,请举个例子!

【问题讨论】:

  • 线程是否真正同时运行取决于硬件(您的硬件是否有多个独立的执行核心)和操作系统(操作系统是否同时调度两个线程)。跨度>
  • “同时”通常表示“显然同时”;在这一刻,我正在写这篇评论,“虽然” wget 正在下载一些东西,“同时”...... ps 列出了 50 个左右正在运行的“东西”......显然都是在“同一时间”;如果有些可以真正并行,这取决于...尽管如此,几乎所有这些都可以“在硬件上并行”,但是从用户的角度来看,我们仍然认为多任务处理是“同时”执行的事情,即使我们在单核 cpu 上运行它。我相信 OPer 的意图是用 fork 和 threads 实现可能

标签: c multithreading function


【解决方案1】:

你会使用线程。

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

您可以查看pthreads tutorial 了解更多详情。

这是一个从this tutorial 生成 pthread 的程序示例。

#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);
}

除了(您可能知道)“完全相同的时间”在技术上是不可能的。无论您是在单核还是多核进程上运行,您都受操作系统调度程序的摆布来运行您的线程。不能保证它们会“同时”运行,相反,它们可能会共享一个内核。您可以启动两个线程并同时运行它们,但这可能不是您所追求的......

【讨论】:

  • 没有链接可以下载吗?是默认包含的吗?
  • 如果你在 linux 上,是的,不知道还有什么。谷歌有一些下载它。
  • @Daniel:您可能已经有可用的 pthreads - 如果您说明您使用的操作系统和编程环境会有所帮助。
  • 这是一个 linux 标准库,虽然有一个半完整的 win32 port。众所周知,跨平台线程在 C 中很烦人,但 Boost 有一个用于 C++ 的 Thread
  • 对不起,我用的是 mac os x 10.6
【解决方案2】:

不可能以标准方式进行。您需要依赖一些第三方方法。即pthreadsWin32 threads

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 示例:

#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;
}

如果您想在两个平台上都使用 POSIX,可以使用 Windows 端口。

【讨论】:

  • 3天寻找简单易懂的东西,终于找到你了。谢谢你没有让我们的生活复杂化
  • win32 main()函数有错误:请把这个:thread2 = CreateThread(NULL, 0, print_once, "Bar", 0, NULL);改成这个:thread2 = CreateThread(NULL, 0, print_twice, "Bar", 0, NULL);
【解决方案3】:

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

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

【讨论】:

    猜你喜欢
    • 2020-09-01
    • 2017-07-26
    • 1970-01-01
    • 1970-01-01
    • 2021-08-13
    • 1970-01-01
    • 1970-01-01
    • 2021-07-20
    相关资源
    最近更新 更多