Peterson的N处理算法

时间:2017-12-01 04:30:36

标签: c++ multithreading algorithm operating-system semaphore

我需要对我的代码提供反馈意见,我是否正确?

问题陈述:

一个。实现一个具有private int和三个公共方法的信号量类:init,wait和signal。等待和信号方法应该从信号量中按预期运行,并且必须在其实现中使用Peterson的N过程算法。

湾编写一个程序,创建5个线程,同时更新共享整数的值,并使用a)中创建的信号量类对象,以确保并发更新的正确性。

这是我的工作计划:

#include <iostream>
#include <pthread.h>

using namespace std;

pthread_mutex_t mid;                   //muted id
int shared=0;                           //global shared variable
class semaphore {
   int counter;
public:
   semaphore(){
   }
   void init(){
       counter=1;                     //initialise counter 1 to get first thread access
   }
   void wait(){
       pthread_mutex_lock(&mid);         //lock the mutex here
       while(1){
           if(counter>0){             //check for counter value
               counter--;             //decrement counter
               break;                   //break the loop
           }

       }
       pthread_mutex_unlock(&mid);       //unlock mutex here
   }
   void signal(){
       pthread_mutex_lock(&mid);       //lock the mutex here
           counter++;                   //increment counter
           pthread_mutex_unlock(&mid);   //unlock mutex here
       }

};
semaphore sm;
void* fun(void* id)
{
   sm.wait();                           //call semaphore wait
   shared++;                           //increment shared variable
   cout<<"Inside thread "<<shared<<endl;
   sm.signal();                       //call signal to semaphore
}


int main() {

   pthread_t id[5];                   //thread ids for 5 threads
   sm.init();
   int i;
   for(i=0;i<5;i++)                   //create 5 threads
   pthread_create(&id[i],NULL,fun,NULL);
   for(i=0;i<5;i++)
   pthread_join(id[i],NULL);           //join 5 threads to complete their task
   cout<<"Outside thread "<<shared<<endl;//final value of shared variable
   return 0;
}

1 个答案:

答案 0 :(得分:1)

您需要在等待循环中旋转时释放互斥锁。

测试恰好起作用,因为线程很可能在任何上下文切换之前运行它们的函数开始完成,因此每个线程在下一个甚至开始之前完成。所以你没有对信号量的争论。如果你这样做了,他们就会被一名服务员在保持互斥锁旋转时卡住,阻止任何人进入柜台,从而释放旋转器。

这是一个有效的例子(虽然它可能仍然有一个初始化竞争导致它偶尔无法正确启动)。它看起来更复杂,主要是因为它使用了gcc内置的原子操作。只要您拥有多个核心,就需要这些,因为每个核心都有自己的缓存。宣布计数器“易变”&#39;只对编译器优化有帮助 - 对于什么是有效的SMP,缓存一致性需要跨处理器缓存失效,这意味着需要使用特殊的处理器指令。您可以尝试用例如替换它们。计数器++和计数器 - (和#39;共享&#39;相同) - 并观察它在多核CPU上的运行方式。 (有关gcc原子操作的更多详细信息,请参阅https://gcc.gnu.org/onlinedocs/gcc-4.8.2/gcc/_005f_005fatomic-Builtins.html

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <stdint.h>


class semaphore {
    pthread_mutex_t lock;
    int32_t counter;
public:
   semaphore() {
       init();

   }
   void init() {
       counter = 1;           //initialise counter 1 to get first access
   }

   void spinwait() {
       while (true) {
           // Spin, waiting until we see a positive counter
           while (__atomic_load_n(&counter, __ATOMIC_SEQ_CST) <= 0)
               ;

           pthread_mutex_lock(&lock);
           if (__atomic_load_n(&counter, __ATOMIC_SEQ_CST) <= 0) {
               // Someone else stole the count from under us or it was
               // a fluke - keep trying
               pthread_mutex_unlock(&lock);
               continue;
           }
           // It's ours
           __atomic_fetch_add(&counter, -1, __ATOMIC_SEQ_CST);
           pthread_mutex_unlock(&lock);
           return;
       }
   }

   void signal() {
       pthread_mutex_lock(&lock);   //lock the mutex here
       __atomic_fetch_add(&counter, 1, __ATOMIC_SEQ_CST);
       pthread_mutex_unlock(&lock); //unlock mutex here
   }

};

enum { 
    NUM_TEST_THREADS = 5,
    NUM_BANGS = 1000
 };

// Making semaphore sm volatile would be complicated, because the
// pthread_mutex library calls don't expect volatile arguments.

int shared = 0;       // Global shared variable
semaphore sm;         // Semaphore protecting shared variable

volatile int num_workers = 0;   // So we can wait until we have N threads


void* fun(void* id)
{
    usleep(100000);                   // 0.1s. Encourage context switch.


    const int worker = (intptr_t)id + 1;

    printf("Worker %d ready\n", worker);

    // Spin, waiting for all workers to be in a runnable state.  These printouts
    // could be out of order.
    ++num_workers;
    while (num_workers < NUM_TEST_THREADS)
        ;

    // Go!

    // Bang on the semaphore.  Odd workers increment, even decrement.
    if (worker & 1) {
        for (int n = 0; n < NUM_BANGS; ++n) {
            sm.spinwait();
            __atomic_fetch_add(&shared, 1, __ATOMIC_SEQ_CST);
            sm.signal();
        }
    } else {
        for (int n = 0; n < NUM_BANGS; ++n) {
            sm.spinwait();
            __atomic_fetch_add(&shared, -1, __ATOMIC_SEQ_CST);
            sm.signal();
        }
    }

    printf("Worker %d done\n", worker);

    return NULL;
}


int main() {

    pthread_t id[NUM_TEST_THREADS]; //thread ids

    // create test worker threads
    for(int i = 0; i < NUM_TEST_THREADS; i++)
        pthread_create(&id[i], NULL, fun, (void*)((intptr_t)(i)));

    // join threads to complete their task
    for(int i = 0; i < NUM_TEST_THREADS; i++)
        pthread_join(id[i], NULL);

    //final value of shared variable.  For an odd number of
    // workers this is the loop count, NUM_BANGS
    printf("Test done.  Final value: %d\n", shared);
    const int expected = (NUM_TEST_THREADS & 1) ? NUM_BANGS : 0;
    if (shared == expected) {
        puts("PASS");
    } else {
        printf("Value expected was: %d\nFAIL\n", expected);
    }

    return 0;
}