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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T08:33:03+00:00 2026-06-08T08:33:03+00:00

I am a bit new to multi threading, so forgive me if these questions

  • 0

I am a bit new to multi threading, so forgive me if these questions are too trivial.

My application needs to create multiple threads in a thread and perform actions from each thread.

For example, I have a set of files to read, say 50 and I create a thread to read these files using CreateThread() function.

Now this main thread creates 4 threads to access the file. 1st thread is given file 1, second file 2 and so on.

After 1st thread completed reading file 1 and gives main thread the required data, main thread needs to invoke it with file 5 and obtain data from it. Similar goes for all other threads until all 50 files are read.

After that, each thread is destroyed and finally my main thread is destroyed.

The issue I am facing is:

1) How to stop a thread to exit after file reading?

2) How to invoke the thread again with other file name?

3) How would my child thread give information to main thread?

4) After a thread completes reading the file and returns the main thread a data, how main thread would know which thread has provided the data?

Thanks

  • 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-08T08:33:04+00:00Added an answer on June 8, 2026 at 8:33 am

    This is a very common problem in multi-threaded programming. You can view this as a producer-consumer problem: the main thread “produces” tasks which are “consumed” by the worker threads (s. e.g. http://www.mario-konrad.ch/blog/programming/multithread/tutorial-06.html) . You might also want to read about “thread pools”.

    I would highly recommend to read into boost’s Synchronization (http://www.boost.org/doc/libs/1_50_0/doc/html/thread.html) and use boost’s threading functionality as it is platform independent and good to use.

    To be more specific to your question: You should create a queue with operations to be done (usually it’s the same queue for all worker threads. If you really want to ensure thread 1 is performing task 1, 5, 9 … you might want to have one queue per worker thread). Access to this queue must be synchronized by a mutex, waiting threads can be notified by condition_variables when new data is added to the mutex.

    1.) don’t exit the thread function but wait until a condition is fired and then restart using a while ([exit condition not true]) loop

    2.) see 1.

    3.) through any variable to which both have access and which is secured by a mutex (e.g. a result queue)

    4.) by adding this information as the result written to the result queue.

    Another advice: It’s always hard to get multi-threading correct. So try to be as careful as possible and write tests to detect deadlocks and race conditions.

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

Sidebar

Related Questions

I am bit new to C#. I am working on a Database application and
I'm not the best with multi-threading, and this is a bit complicated, so hopefully
I'm learning Java and was able to do a bit of multi-threading with my
This is a multi threaded scenario. The main thread handles the application and UI
My question might sound a bit naive but I'm pretty new with multi-threaded programming.
I have a multi-threaded server that handles client requests, and makes new threads for
Stuff I've already figured out I'm learning how to create a multi-tenant application in
I'm a bit new to linq and I'm unsure if what I desire is
I'm a bit new to Rails/RSpec/Capybara, so this is probably a newbie question, but
I'm a bit new to wordpress plugin coding. I've been working on a plugin

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.