views:

111

answers:

3

i have two threads, they run pretty fast, i'm using pthread_mutex_lock and pthread_mutex_unlock to access global (externed variables) data

the problem is that my application takes about 15-20% of CPU running on Ubuntu Linux,

the same code but with EnterCriticalSection and LeaveCriticalSection and running on Windows uses 1-2% of CPU

A: 

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.

wilx
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
@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
tested with PTHREAD_MUTEX_NORMAL, same thing, are there any faster?
Tenev
I Just want the Mutex Locking/Unlocking to work with the current Process only, not for all Processes < which i think is the reason making it slower
Tenev
+5  A: 

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.

Nikolai N Fetissov
+1  A: 

I can't quite tell what your question is, but let's say it's "How do I make the Linux version faster?".

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.

Mark B
its the mutex, it takes longer to lock/unlock than criticalsections... is there a way to make the mutex working on the current process memory only, not with all processes? i read somewhere that mutex works with kernel and it can be locked from different processes
Tenev