Jump to content

lock c++

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
Guest_h4x_*

Guest_h4x_*
  • Guests
how do insert lock instruction befofre incrementation?
lock c++, something like that.

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
lock isn't a keyword in C/C++. Are you trying to apply an ASM concept?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Guest_h4x_*

Guest_h4x_*
  • Guests
i am trying to increment 4 bytes using multiple threads.
so whats the c construction for lock?

#4
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,716 posts
The thread library you're using should provide some sort of mutex construction. Look at the documentation to see. Are you using pthreads or Windows API to create your threads? I can help you with either.

As far as the C construction for lock, there is none unless you're absolutely sure that your program will run only on Intel processors. In this case you'll have to write your increment instruction entirely in ASM, but that depends on the compiler. On a Microsoft compiler you can do this:

//32-bit processors only
__asm    lock inc    myvar;
//32- and 64-bit processors
__asm    lock add    myvar,1;

I forget the exact syntax for GNU GCC, but it's something like:

asm("lock add    myvar,1;","","");
Don't quote me on that.

Also, make sure you declare that variable as volatile.

Edited by dargueta, 11 September 2009 - 04:26 AM.
Fixed formatting

sudo rm -rf /

#5
Guest_h4x_*

Guest_h4x_*
  • Guests
Ok nvm, for me c is an inferior language wich i use only when im too tired to write in asm, such details as atomic access do not concern me.

#6
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,716 posts
Wow.
sudo rm -rf /