i want to print odd and even numbers from 2 separate threads in synchronize manner in win32 C++. i am using critical section to achieve it, but cn't get the desired result. can be do it using critical section.
Asked
Active
Viewed 1,722 times
-4
-
Also a clear description of what `synchronized` means to you could help. Do you mean that the output should be `1,2,3,4...` and each thread is generating either the even or odd numbers? – David Rodríguez - dribeas Jan 15 '13 at 16:10
-
4The point of synchronization is so things can happen out of order. I don't recommend this exercise. – djechlin Jan 15 '13 at 16:11
-
yes, one thread will print even number, while other print odd number. output should be like 1,2,3,4.... – user1808932 Jan 15 '13 at 16:12
-
1It's more challenging than it sounds at first glance. Please post up what you have tried and we will help you from there. – Chad Jan 15 '13 at 16:52
-
@franji:it's not a homework, just for fun, i have done earlier using mutex, tried using critical section, i find my problem in my implementation and resolve it and pasted the code n it's works fine now. – user1808932 Jan 15 '13 at 18:24
-
you might check http://stackoverflow.com/questions/14641134/printing-odd-and-even-number-printing-alternately-using-threads-in-c/16272239#16272239 – Vijay Apr 29 '13 at 06:12
-
@vijay: ur solution is not up to the mark, since ur code is printing the values only through even_thread_cs, since ur sharedvariable value always remain 0. – user1808932 Apr 30 '13 at 21:22
-
what is real question? why it is not real question? Actually it is hot interview question. – Vijay May 16 '13 at 10:47
1 Answers
2
I believe it can be done with critical section only, but in a way that is not obvious.
// all in pseudocode:
mutex printMutex;
int printedOdd = 1;
//thread 1
int start = 1;
while(1) {
lock (printMutex);
if (printOdd == 1) {
print ( start );
start += 2;
printedOdd = 0;
} else {
unlock (printMutex);
yield();
continue;
}
unlock (printMutex);
}
// thread 2
int start = 2;
while(1) {
lock (printMutex);
if (printOdd == 0) {
print ( start );
start += 2;
printedOdd = 1;
} else {
unlock (printMutex);
yield();
continue;
}
unlock (printMutex);
}
I do not actually understand the point of doing something like this, but an answer is... an answer:)

Dariusz
- 21,561
- 9
- 74
- 114
-
i want to implement it through critical section, not through mutex. – user1808932 Jan 15 '13 at 16:57
-
2
-
@david:as far as i know, mutex can be used across inter process and intra process thread synchronization while critical section can be used only synchronize threads in a single process. – user1808932 Jan 15 '13 at 17:08
-
@user1808932 - "i am using critical section to achieve it, but cn't get the desired result." - Post your code and people will help. – Caribou Jan 15 '13 at 17:18
-
A Win 32 critical section object is a mutex, where I use the generic terminology: http://en.wikipedia.org/wiki/Mutual_exclusion Did you even note that this answer gives you pseudo code. When you turn it into real code, you can use the mutex of your choice. – David Heffernan Jan 15 '13 at 17:24
-
@david:can u suggest me how to implement yield in win32. btw i have resolved my problem, n pasted the working code with the query. – user1808932 Jan 15 '13 at 18:28