简体   繁体   English

从主线程C ++更新信号量

[英]Updating semaphore from main thread C++

I am playing around with semaphores and get myself into this problem. 我正在玩信号灯,并使自己陷入这个问题。 What I am trying to do is updating the semaphore inside the pthread from the main thread. 我想做的是从主线程更新pthread内部的信号量。 I created the struct , passed the semaphore with that struct into the pthread and used the struct in another function to do sem_post . 我创建了该struct ,并将带有该结构的semaphore传递到pthread中,并在另一个函数中使用该结构执行sem_post But the following code is not working. 但是以下代码不起作用。 It seems that semaphore I passed into the pthread and the one I used in start function are different. 看来我传递给pthread的信号量与我在start函数中使用的信号量不同。 There are a lot of examples which create semaphores globally and use in threads. 有很多示例可以全局创建信号量并在线程中使用。 But I would like to create semaphore locally so that if needed, I can create multiple semaphores and multiple threads to synchronize. 但是我想在本地创建信号灯,以便在需要时可以创建多个信号灯和多个线程进行同步。 How should I approach this problem? 我应该如何解决这个问题?

#include <iostream> 
#include <semaphore.h> 
#include <pthread.h> 
#include <signal.h>
#include "unistd.h"

using namespace std; 

class m { 
    public: 
        struct my_sem { 
            sem_t sem_lock; 
        };  
        m(); 
        ~m(); 

        void create_threads(); 
        void start(my_sem*); 
        static void *hello(void *);  
}; 

m::m() { 
    create_threads(); 
} 

m::~m() {}

void m::create_threads() { 
    pthread_t t1, t2; 
    sem_t s; 

    sem_init(&s, 0, 0); 

    my_sem *sem1 = new my_sem; 
    sem1->sem_lock = s; 

    pthread_create(&t1, null, hello, sem1); 
    start(sem1); 
} 

void *m::hello(void *arg) { 
    my_sem* a = (my_sem*)arg; 
    sem_t t = a->sem_lock; 

    while(1) { 
        sem_wait(&t); 
        cout << "hello" << endl; 
    } 
} 

void m::start(my_sem* s) { 
    sem_t h = s->sem_lock; 
    while(1) { 
        sleep(10); 
        sem_post(&h); 
    } 
} 

int main() { 

    m m;     
    return 0; 
} 

The Output I am expecting here is: 我在这里期望的输出是:

HELLO 
<wait 10 seconds> 
HELLO 
<wait 10 seconds> 
.
.
.

You don't copy semaphores as they rely on their address (location in the memory). 不必复制信号量,因为它们依赖于它们的地址(在内存中的位置)。

Assigning one sem_t to another, will not share the state between them. 将一个sem_t分配给另一个,不会在它们之间共享状态。 The assignment will copy some values, but you will have two distinct semaphore objects. 该分配将复制一些值,但是您将有两个不同的信号量对象。

Instead, you create a single semaphore as a member of some class and pass pointers to it, like this: 相反,您可以创建一个信号量作为某些类的成员,然后将其传递给指针,如下所示:

void m::create_threads() { 
    pthread_t t1, t2; 

    sem_init(&this->sem.sem_lock, 0, 0); 

    pthread_create(&t1, nullptr, hello, &this->sem); 
    start(&this->sem);
} 

void *m::hello(void *arg) { 
    my_sem* a = (my_sem*)arg; 

    while(1) { 
        sem_wait(&a->sem_lock); 
        cout << "hello" << endl; 
    } 
} 

void m::start(my_sem* s) { 
    while(1) { 
        sleep(1); 
        sem_post(&s->sem_lock); 
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM