c - Concurrency with pthreads, a typical misbehaviour -


I'm not sure what's wrong with the program below, but it does not print once every language, but Randomly few more times, fewer and few wont print

  #include   

I think the problem is related to the logic indicator in some way. I tried to lock different parts but there was no success.

You are passing the address of the variable i , which is the main Thread keeps changing so you are at the mercy of scheduling: can someone change your code before it is able to change any i ? Instead, you can try to cross the string directly, very simple code:

  RC = pthread_create (and thread [i], NULL, PrintHello, Message [i]); Zero * PrintHello (Zero * arg) {char * msg = arg; Printf ("% s \ n", msg); Return tap; }   

There is another, poor option where you pass the actual code in the form of logic (not its address) as i .

Comments