Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

print m1 m2 m3 in 3 different threads in sequence

 
Faisal syed
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Hi,
THe output Iam getting now is

Thread Name is = Thread M1 m1
Thread Name is =Thread M1 m2
Thread Name is =Thread M1 m3
Thread Name is = Thread M2 m1
Thread Name is =Thread M2 m2
Thread Name is =Thread M2 m3
Thread Name is = Thread M3 m1
Thread Name is =Thread M3 m2
Thread Name is =Thread M3 m3


However, I want thread2 to print m1 after thread 1 prints m1, and thread 3 to print m1 after thread 2.
I mean, the desired output is

m1 (code run by thread 1)

m1 (code run by thread 2)

m1 (code run by thread 3)

m2 (code run by thread 1)

m2 (code run by thread 2)

m2 (code run by thread 3)

m3 (code run by thread 1)

m3 (code run by thread 2)

m3 (code run by thread 3)

Thanks
Faisal
 
Faisal syed
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We cannot use join() as well since after thread 1, 2 and 3 print m1 in this order, they again need to print m2, m3.

Please advise.

Thanks
Faisal
 
Henry Wong
author
Marshal
Pie
Posts: 20997
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

If you need an exact sequence, then either consider doing it single threaded, or add enough share state so that a thread will only run when it is its turn only.

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic