C语言 C:同时运行两个函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3051009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C: Run two functions at the same time?
提问by Daniel
I have two functions in C:
我在 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!
我将如何同时运行这两个函数?如果可能,请举例说明!
回答by Stephen
You would use threads.
你会使用线程。
For example, pthreads is a c library for multithreading.
例如,pthreads 是用于多线程的 ac 库。
You can look at this pthreads tutorialfor more details.
您可以查看此pthreads 教程以获取更多详细信息。
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...
除了(您可能知道)“完全相同的时间”在技术上是不可能的。无论您是在单核还是多核进程上运行,您都受操作系统调度程序的支配来运行您的线程。不能保证它们会“同时”运行,相反,它们可能会共享一个核心。您可以启动两个线程并同时运行它们,但这可能不是您想要的......
回答by GManNickG
It's not possible to do in a standard way. You'll need to rely on some third-party method. Namely, pthreadsor 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 端口。
回答by James
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.
如果您需要确保这两个函数以某种同步方式执行操作,您将需要了解同步原语,例如互斥锁、信号量和监视器。

