C 多线程启动顺序

C 多线程启动顺序,c,linux,pthreads,C,Linux,Pthreads,我有4个线程来创建thread1、thread2、thread3和thread4: pthread_create(thread1,NULL,thread_func1,NULL); pthread_create(thread2,NULL,thread_func2,NULL); pthread_create(thread3,NULL,thread_func3,NULL); pthread_create(thread4,NULL,thread_func4,NULL); 在调试中,启动线程的顺序与源代

我有4个线程来创建thread1、thread2、thread3和thread4:

pthread_create(thread1,NULL,thread_func1,NULL);
pthread_create(thread2,NULL,thread_func2,NULL);
pthread_create(thread3,NULL,thread_func3,NULL);
pthread_create(thread4,NULL,thread_func4,NULL);
在调试中,启动线程的顺序与源代码中定义的不同。 有没有一种解决方案可以按照我可以定义的顺序启动线程?

启动顺序是连续的,因为create调用是按写入顺序进行的

但是,调度程序出于任何原因都没有按照您希望的顺序调度新启动的线程。如果顺序很重要,也许线程不是你想要的?线程的最大优点是它们并不总是按顺序进行调度

如果确实需要,您可以使用同步原语(例如,一系列互斥体或condvar)来确保在某一点上以可预测的顺序发生,但从那一点起,顺序仍将取决于调度程序的突发奇想。例如,此代码保证每个线程将按创建顺序打印其ID:

#include <pthread.h>
#include <stdio.h>

static pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

void sync_threads(const int num, int *cur) {
  pthread_mutex_lock(&mut);
  while (*cur != num) {
    pthread_cond_wait(&cond, &mut);
  }
  // Do work that must happen in order here:
  printf("Thread: %d\n", num);
  ++*cur;
  pthread_mutex_unlock(&mut);
  pthread_cond_broadcast(&cond);
}

static int num = 1;

void *thread1(void *d) {
  sync_threads(1,&num);
  while (1); // Rest of work happens whenever
  return NULL;
}

void *thread2(void *d) {
  sync_threads(2,&num);
  while (1);
  return NULL;
}

void *thread3(void *d) {
  sync_threads(3,&num);
  while (1);
  return NULL;
}

void *thread4(void *d) {
  sync_threads(4,&num);
  while (1);
  return NULL;
}

int main() {
  pthread_t t1,t2,t3,t4;
  pthread_create(&t1, NULL, thread1, NULL);
  pthread_create(&t2, NULL, thread2, NULL);
  pthread_create(&t3, NULL, thread3, NULL);
  pthread_create(&t4, NULL, thread4, NULL);
  while(1) {
    // some work
  }
}

因此,在创建线程时,您并不关心哪一个线程真正获得了先运行的机会。

我认为您并不真正关心先执行哪个线程。如果您只需要四个线程的唯一标识符,请检查pthread_self。要获得顺序ID,请从线程内调用ID分配器;或者在调用pthread_create时生成ID并将其作为用户参数传递

Move 'pthread_create(thread2,NULL,thread_func2,NULL);' into thread_func1()
Move 'pthread_create(thread3,NULL,thread_func2,NULL);' into thread_func2()
Move 'pthread_create(thread4,NULL,thread_func2,NULL);' into thread_func3()

这与最近发布的另一个问题非常接近,而且也是错误的奇怪的“

在我使用的解决方案之后

#include <pthread.h>
#include <stdio.h>

static pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
static bool wait = TRUE;

void thread_sync() {
  pthread_mutex_lock(&mut);
  wait = FALSE;
  pthread_cond_signal(&cond);
  pthread_mutex_unlock(&mut);
}
void thread_wait_sync() {
  pthread_mutex_lock(&mut);
  if (wait==TRUE)
  {
      pthread_cond_wait(&cond,&mut);
  }
  wait = TRUE;
  pthread_mutex_unlock(&mut);
}

void *thread1(void *d) {
  thread_sync();
  while (1); // Rest of work happens whenever
  return NULL;
}

void *thread2(void *d) {
  thread_sync();
  while (1);
  return NULL;
}

void *thread3(void *d) {
  thread_sync();
  while (1);
  return NULL;
}

void *thread4(void *d) {
  while (1);
  return NULL;
}

int main() {
  pthread_t t1,t2,t3,t4;
  pthread_create(&t1, NULL, thread1, NULL);
  thread_wait_sync();
  pthread_create(&t2, NULL, thread2, NULL);
  thread_wait_sync();
  pthread_create(&t3, NULL, thread3, NULL);
  thread_wait_sync();
  pthread_create(&t4, NULL, thread4, NULL);
  while(1) {
    // some work
  }
}
#包括
#包括
静态pthread\u mutex\u t mut=pthread\u mutex\u初始值设定项;
静态pthread_cond_t cond=pthread_cond_初始值设定项;
静态布尔等待=真;
无效线程\u sync(){
pthread_mutex_lock(&mut);
等待=错误;
pthread_cond_信号(&cond);
pthread_mutex_unlock(&mut);
}
无效线程\u等待\u同步(){
pthread_mutex_lock(&mut);
if(wait==TRUE)
{
pthread_cond_wait(&cond,&mut);
}
等待=真;
pthread_mutex_unlock(&mut);
}
void*thread1(void*d){
线程同步();
while(1);//其余的工作在
返回NULL;
}
void*thread2(void*d){
线程同步();
而(1),;
返回NULL;
}
void*thread3(void*d){
线程同步();
而(1),;
返回NULL;
}
void*thread4(void*d){
而(1),;
返回NULL;
}
int main(){
pthread_t t1、t2、t3、t4;
pthread_create(&t1,NULL,thread1,NULL);
线程等待同步();
pthread_create(&t2,NULL,thread2,NULL);
线程等待同步();
pthread_create(&t3,NULL,thread3,NULL);
线程等待同步();
pthread_create(&t4,NULL,thread4,NULL);
而(1){
//一些工作
}
}

重复@杰伊-我想我倾向于同意他们是重复的,这一个是前一个的轻微概括,我的答案基本上就是这个答案的概括。奇怪的是,两个如此相似的问题被问得如此接近……通常,除非在日志文件的每一行上都有一个时间戳,否则无法从日志文件中推断出多少执行顺序。IO是静音的,因此线程仅以与锁获取相关的任意顺序执行
printf
。+1表示“同步的东西越多,线程的重要性就越小。”
#include <pthread.h>
#include <stdio.h>

static pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
static bool wait = TRUE;

void thread_sync() {
  pthread_mutex_lock(&mut);
  wait = FALSE;
  pthread_cond_signal(&cond);
  pthread_mutex_unlock(&mut);
}
void thread_wait_sync() {
  pthread_mutex_lock(&mut);
  if (wait==TRUE)
  {
      pthread_cond_wait(&cond,&mut);
  }
  wait = TRUE;
  pthread_mutex_unlock(&mut);
}

void *thread1(void *d) {
  thread_sync();
  while (1); // Rest of work happens whenever
  return NULL;
}

void *thread2(void *d) {
  thread_sync();
  while (1);
  return NULL;
}

void *thread3(void *d) {
  thread_sync();
  while (1);
  return NULL;
}

void *thread4(void *d) {
  while (1);
  return NULL;
}

int main() {
  pthread_t t1,t2,t3,t4;
  pthread_create(&t1, NULL, thread1, NULL);
  thread_wait_sync();
  pthread_create(&t2, NULL, thread2, NULL);
  thread_wait_sync();
  pthread_create(&t3, NULL, thread3, NULL);
  thread_wait_sync();
  pthread_create(&t4, NULL, thread4, NULL);
  while(1) {
    // some work
  }
}