Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8463437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:27:14+00:00 2026-06-10T14:27:14+00:00

I am writing a consumer/producer class with a simple semaphore implementation for c++11. However,

  • 0

I am writing a consumer/producer class with a simple semaphore implementation for c++11. However, the following code fails to compile. If I remove the producer_consumer class and make producer and consumer global functions, and move thread creation part (std::thread t1(consumer), t2(consumer); t1.join(); t2.join();)to the main function, it compiles but the implementation is still not correct and will eventually lead into segmentation fault. How can I correct the code? thanks.

#include <iostream>
#include <thread>
#include <mutex>

class semaphore {
private:
    std::mutex m;
    std::condition_variable cond;
    unsigned long n;
public:
    semaphore(int i) : n(i) {}

    void up() {
        std::unique_lock <std::mutex> lock (m);
        ++n;
        cond.notify_one();
    }

    void down() {
        std::unique_lock <std::mutex> lock (m);
        while(!n) cond.wait(lock);
        --n;
    }
};
class producer_consumer{
private:
    semaphore full, empty;
    int i = 0;
    std::thread t1, t2;
public:
    producer_consumer(int n): full(0), empty(n), i(0){}
    void run(){
        t1 = std::thread(&producer_consumer::producer, *this); 
        t2 = std::thread(&producer_consumer::consumer, *this);
    }
    void stop(){
        t1.join();
        t2.join();
    }
    void producer (){
        while (true){
            empty.down();
            i ++;
            std::cout << "[p]" << i << std::endl;
            full.up();  
        }
    }
    void consumer (){
        while (true){
            full.down();
            i --;
            std::cout << "[c]" << i << std::endl;
            empty.up(); 
        }
    }
};

int main(){
    producer_consumer pc(5);
    pc.run();
    pc.stop();
    return 0;
}

I use clang++ to compile the file:

clang++ -std=c++0x -stdlib=libc++ pc.cpp ; ./a.out

error message:

In file included from file_name.cpp:1:
In file included from /usr/bin/../lib/c++/v1/iostream:38:
In file included from /usr/bin/../lib/c++/v1/ios:216:
In file included from /usr/bin/../lib/c++/v1/__locale:15:
In file included from /usr/bin/../lib/c++/v1/string:434:
In file included from /usr/bin/../lib/c++/v1/algorithm:591:
/usr/bin/../lib/c++/v1/type_traits:1423:12: error: call to implicitly-deleted
      copy constructor of 'typename decay<producer_consumer &>::type'
      (aka 'producer_consumer')
    return _VSTD::forward<_Tp>(__t);
           ^~~~~~~~~~~~~~~~~~~~~~~~

Update: @DanqiWang solved the compilation problem by changing *this to this.
Now it seems that the semaphore is not working properly and finally will crash the program:

./a.out
[p]1
[p]2
[p]3
[p]4
[p]5
....
[p]2
[p]3
[p]4
1
[c]3
[p[]c3]
3
[[c]3
p][2c
][p]3
[p]4
4
[c]3
[c]2
Segmentation fault: 11
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T14:27:16+00:00Added an answer on June 10, 2026 at 2:27 pm

    I have the following suggestions for your code:

    • Although iostream becomes thread-safe in C++11, it should be locked to avoid mangled output.
    • use std::atomic instead of naked int.
    • If there is a segment fault, dump a core, use gdb to debug it, or post the stack trace here.

    Hope these help.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing two applications, a producer and a consumer. My code is nearly
Recently, I've begun writing my own PHP OpenID consumer class in order to better
I'm writing an application that has a multiple producer, single consumer model (multiple threads
I have tried to create a concurrent queue class, in a producer-consumer pattern. I
I'm writing a program with a consumer thread and a producer thread, now it
I have this producer / consumer code : MAIN : static void Main() {
I'm writing a win32 library and I need to implement a producer-consumer queue using
Consider this producer-consumer code using a ArrayBlockingQueue : I want to find out a.
I have a producer-consumer scenario in ASP.NET. I designed a Producer class, a Consumer
i have this scenario: class MyClass { Producer p; Consumer c; public static void

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.