jagttt
jagttt

Reputation: 1070

VS rand() problem with pthread-win32

I come into a strange problem in pthread programming I've compiled the following code in vs2005 with pthread-w32

#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <pthread.h>
#include <windows.h>

pthread_mutex_t lock;

void* thread1(void *) {
  int r1;
  while(true) {
    pthread_mutex_lock(&lock); // rand is maybe a CS
    r1 = rand() % 1500;
    pthread_mutex_unlock(&lock);
    Sleep(r1); printf("1:%d\n", r1);
  }
  return NULL;
}

void* thread2(void *) {
  int r2;
  while(true) {
    pthread_mutex_lock(&lock);
    r2 = rand() % 1500;
    pthread_mutex_unlock(&lock);
    Sleep(r2); printf("2:%d\n", r2);
  }
  return NULL;
}

int main() {
  srand((int)time(NULL));
  pthread_mutex_init(&lock, NULL);

  pthread_t tc_p, tc_v;
  pthread_create(&tc_p, NULL, thread1, NULL);
  pthread_create(&tc_v, NULL, thread2, NULL);

  pthread_join(tc_p, NULL);
  pthread_join(tc_v, NULL);

  pthread_mutex_destroy(&lock);

    return 0;
}

and output is like this

2:41
1:41
1:467
2:467
1:334
2:334
1:1000
2:1000

it's just like that rand() is return the same result in every two calls and i have srand() but the result doesn't change each time i run the program

i'm very new to multi thread programming and i've heard about the rand() is not thread safe. but i still can't figure out if the program above is wrong or the rand() function has some problem in it.

Upvotes: 1

Views: 1867

Answers (2)

rlbond
rlbond

Reputation: 67789

Try using rand_s() instead, it is thread-safe. See here. Of course, it's not portable.

Upvotes: 1

RichieHindle
RichieHindle

Reputation: 281505

rand is only pseudo-random, and will return the same sequence each time. srand only works on the current thread, so calling it in your main thread won't affect your worker threads.

You need to call srand from within each thread, with a value that's different for each thread - for instance, within your thread1 and thread2 functions:

srand((int)time(NULL) ^ (int)pthread_getthreadid_np());

Upvotes: 5

Related Questions