문제

I'm working on solving the Producer/Consumer problem through different methods. The one I'm currently working on involves my own implementation of a semaphore, paired with Peterson's solution to protect the semaphore's down() and up() function calls.

The problem is, I'm working in C, and the only way I can think of creating a semaphore would be to use a struct with function pointers for down() and up(). Is this the right idea?

도움이 되었습니까?

해결책

You will likely want to use some OS-provided or hardware-provided synchronization primitive. Examples include system calls (e.g. Linux's futex call, Windows' EnterCriticalSection), hardware swap calls (e.g. cmpxchg on x86, ldrex/strex on ARM), or library functions which use one of these methods (e.g. pthread_mutex).

Trying to do synchronization without any help from the OS or hardware is likely to be quite difficult.

다른 팁

Check futex, it is a user space syn primitive. But I don't think you're looking for something this much complicated.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top