multi thread launch arrangement

I have 2 thread to create thread1 and thread2. When creating threads with:

pthread_create(&thread1, NULL, &function_th1, NULL);
pthread_create(&thread2, NULL, &function_th2, NULL);

The thread2 is launched before the thread1 and I'm looking for a solution to start thread1 before thread2.

Not looking for this solution:

pthread_create(&thread2, NULL, &function_th2, NULL);
pthread_create(&thread1, NULL, &function_th1, NULL);

Upvotes: 2

Views: 181

Answers (3)

MOHAMED
MOHAMED

Reputation: 43518

here after the solution I suggest

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

Upvotes: 1

Martin James
Martin James

Reputation: 24847

Move the 'pthread_create(&thread2, NULL, &function_th2, NULL);' to the top of the 'function_th1' function.

That will certainly achieve what you ask for, no complex signalling required.

Whether what you ask for is what you actually want or need is another matter.

Upvotes: 3

Tudor
Tudor

Reputation: 62439

There is no relation between when a thread creation call is issued and when the thread actually starts executing. It all depends on implementation, OS, etc. that's why you are seeing the two threads actually starting in a seemingly random order.

If you need thread 1 to start before thread 2 you probably have some data/logical dependency on some event. In this case you should use a condition variable to tell thread 2 when to actually begin executing.

// condition variable
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
// associated mutex
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
// state for condition variable
int shouldWait = 1;

...

void* function_th1(void* p) {
     // execute something

     // signal thread 2 to proceed
     pthread_mutex_lock(&mutex);
     shouldWait = 0;
     pthread_cond_signal(&cond);
     pthread_mutex_unlock(&mutex);

     // other stuff
}

void* function_th2(void* p) {
     // wait for signal from thread 1
     pthread_mutex_lock(&mutex);
     while(shouldWait) {
         pthread_cond_wait(&cond, &mutex);
     }
     pthread_mutex_unlock(&mutex);

     // other stuff
}    

Upvotes: 5

Related Questions