Thread: mwait/monitor example

  1. #1
    Registered User
    Join Date
    Oct 2017
    Posts
    21

    mwait/monitor example

    Hi,

    I am trying to play around with the mwait and monitor concepts on intel processors (I have a KNL machine so these instructions are available).

    I can't, for the life of me, see how to actually use these instructions. There are some compiler intrinsics __monitor, __mwait, for the intel compilers, but I can't find a single example of how to use these.

    Has anyone here used these before?

    Thanks!

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    The answer to this stackoverflow question is quite extensive. I suggest you read it, it contains everything you'd want to know.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Oct 2017
    Posts
    21
    I did indeed read that, but that solution has an inherent race condition in it, which will certainly lead to buggy code.

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Code:
    struct MonitoredType
    {
      int (*event)(struct MonitoredType const* m);              /*Return 0 to keep monitoring*/
      struct AnyType data;                                /*Less, in size, than MIN_MONITOR_RANGE*/
      char padding[MAX_MONITOR_RANGE - sizeof(AnyType)];
    };
    
    void wait_for_write(struct MonitoredType const* m)
    {
       /* This may miss a write if it happens before MONITOR, beware of race conditions if necessary */
       do
       {
         _mm_monitor(&m->data, 0, 0);
         _mm_mwait(0, 0);
       } while ( ! m->event(m));
    }
    Any use of concurrency and parallelism can lead to buggy code if you don't use it the way it was meant to be used.
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. TCP/Network monitor VS Wireless monitor
    By geek@02 in forum Tech Board
    Replies: 1
    Last Post: 05-29-2017, 01:13 AM
  2. Replies: 3
    Last Post: 04-08-2011, 06:26 AM
  3. Help Me Buy A Monitor
    By SMurf in forum Tech Board
    Replies: 4
    Last Post: 10-13-2006, 12:20 PM
  4. Need A Monitor!!!!!!!!
    By OneStiffRod in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 12-10-2002, 02:24 AM

Tags for this Thread