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 8647881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:10:55+00:00 2026-06-12T13:10:55+00:00

I’m new to threading (and C/C++ for that matter), and I’m attempting to use

  • 0

I’m new to threading (and C/C++ for that matter), and I’m attempting to use multiple threads to access shared variables.

In the main, I’ve created a variable char inputarray[100];

Thread 1: This thread will be reading data from stdin in 2 byte bursts, and appending them to the inputarray. (input by feeding a file in)

Thread 2: This thread will be reading data 1 byte at a time, performing a calculation, and putting its data into an output array.

Thread 3: This thread will be outputting data from the output array in 2 byte bursts. (stdout)

I’ve attempted the input part and got it working by passing a struct, but would like to do it without using a struct, but it has been giving me problems.

If I can get input down, I’m sure I’ll be able to use a similar strategy to complete output. Any help would be greatly appreciated.

Below is a rough template for the input thread.

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

using namespace std;

void* input(void* arg) {
    char reading[3];
    fread(reading,1,2,stdin);

    //append to char inputarray[]..???
}

int main() {
    char inputarray[100];
    pthread_t t1;
    pthread_create(&t1, NULL, &input, &inputarray);
    void *result;
    pthread_join(t1,&result);
    return 0;
}  
  • 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-12T13:10:56+00:00Added an answer on June 12, 2026 at 1:10 pm

    You are on the correct track:

    As a note the pthreads libraries are C libs so you need to declare the callbacks as C functions:

    extern "C" void* input(void* arg);
    

    Personally I would pass the address of the first element:

    pthread_create(&t1, NULL, &input, &inputarray[0]);
    

    This then makes your code look like this:

    void* input(void* arg) {
    
        try
        {
           char*  inputarray    = (char*)arg;
           size_t inputLocation = 0;
    
           // Need to make sure you don't over run the buffer etc...
           while(!finished())
           {
              fread(&inputarray[inputLocation],1,2,stdin);
              inputLocation += 2;
           }
        }
        catch(...){} // Must not let exceptions escape a thread.
        return NULL;
    }
    

    The trouble with this style is that you are putting the responsibility for coordination into each individual thread. The writer thread has to check for end the reader thread has to check there is data available etc. All this needs coordination so now you need some shared mutexes and condition variables.

    A better choice is to move that responsibility into the object the does the communication. So I would create a class that has the basic operations needed for communication then make its methods do the appropriate checks.

     class Buffer
     {
         public:
            void write(......); //
            void read(.....);   //
         private:
            // All the synchronization and make sure the two threads
            // behave nicely inside the object.
     };
    
     int main()
     {
           pthread_t threads[3];
           std::pair<Buffer, Buffer>   comms;
           // comms.first     inputToRead
           // comms.second    processesToOutput
    
    
           pthread_create(&threads[0], NULL, &readInput, &comms.first);   // Input
           pthread_create(&threads[1], NULL, &procInput, &comms);         // Processing
           pthread_create(&threads[2], NULL, &genOutput, &comms.second);  // Output
    
           void *result;
           pthread_join(threads[0],&result);
           pthread_join(threads[1],&result);
           pthread_join(threads[2],&result);
    
     }
    

    As a side note:

    Unless there is something very strange about your processing of data. This would probably be faster written as a single threaded application.

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

Sidebar

Related Questions

I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I am doing a simple coin flipping experiment for class that involves flipping a
I have a French site that I want to parse, but am running into

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.