【发布时间】:2021-01-28 02:22:40
【问题描述】:
我在 c 中创建了两个线程。我想通过每个线程执行两个单独的函数。如何让一个特定的线程首先被执行。
#include <stdio.h>
#include <pthread.h>
void* function_1(void* p)
{
// statements
}
void* function_2(void* p)
{
// statements
}
int main(void)
{
pthread_t id1;
pthread_t id2;
pthread_create(&id1, NULL, function_1, NULL);
pthread_create(&id2, NULL, function_2, NULL);
pthread_exit(NULL);
}
程序启动时如何让function_1在function_2之前执行?
【问题讨论】:
-
是否值得在多线程程序中要求顺序执行?最好让一个单线程程序顺序调用这两个函数。
-
如果你真的需要同步线程,你可以使用一个pthreads condition variable和一个pthreads mutex。
标签: c linux multithreading pthreads