Pthread_mutex_lock and unlock?

That might be a good thing actually - less time spent in wait, more time crunching data.

That might be a good thing actually - less time spent in wait, more time crunching data. CPU percentage calculations are very different on different OS-es. Try measuring your throughput - how many "work items" you are able to process in a unit of time.

One possible path for reducing lock contention (if this is indeed your problem) is to connect producer and consumer threads with a queue. Linking new item onto the queue tail is quick, same for un-linking off the queue head, - couple of pointer operations. STL even has a bunch of containers you can use (std::deque, std::queue, std::list).

You would have to provide your own locking though. Or look into Intel Threading Building Blocks.

". First, are you sure that on Linux you enabled optimization? Assuming yes, are both programs doing the same amount of "work"?

If that's the case, then you need to profile - that will show you directly where the CPU cycles are being used and should enable you to optimize your algorithm/code.

I read somewhere that mutex works with kernel and it can be locked from different processes – Tenev Jul 13 '10 at 20:46.

More important than CPU load is which version does more of real work. It could be that the 1-2% of load translates to less real work done. Measure that instead of the CPU load percentage.

I am testing it with 1000 printf's from 2 threads, the linux program will use more CPU always than the Windows one, the windows program finishes faster... I mean DOUBLE Faster. So its a problem of pthread_mutex_lock/unlock, in the MSDN website it's written that entercriticalsection is faster than mutex... so how do I make mutex working faster which is = to making mutex take less CPU IMO... edit: instead of printf i've put test++; same results, windows program finishes 1.5 times faster than the linux one, + the linux one takes more CPU :( – Tenev Jul 13 '10 at 13:32 @tenev: This sounds more like a problem with the speed of standard IO rather than problem with the speed of mutexes and critical sections. Eliminate the IO and try again.

Also, in case of POSIX threads/mutexes, make sure your pthread_mutex_t is initialized with PTHREAD_MUTEX_NORMAL attribute. It could make a difference if PTHREAD_MUTEX_DEFAULT! = PTHREAD_MUTEX_NORMAL.

– wilx Jul 13 '10 at 13:44 tested with PTHREAD_MUTEX_NORMAL, same thing, are there any faster? – Tenev Jul 13 '10 at 13:55 I Just want the Mutex Locking/Unlocking to work with the current Process only, not for all Processes.

I am testing it with 1000 printf's from 2 threads, the linux program will use more CPU always than the Windows one, the windows program finishes faster... I mean DOUBLE Faster. So its a problem of pthread_mutex_lock/unlock, in the MSDN website it's written that entercriticalsection is faster than mutex. " Your analysis is incorrect.

The time taken in the printf calls completely swamps the lock/unlock time. If you think about, it must be this way. Two concurrent printf calls to the same destination must acquire at least one lock to prevent stomping on the shared destination.So printf must be at least as expensive as a lock acquire/release even ignoring the cost of the output operation.

Found the fastest way, just use pthread rwlocks!

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions