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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:12:58+00:00 2026-05-16T14:12:58+00:00

i have written a simple console application just to try boost::thread, i am a

  • 0

i have written a simple console application just to try boost::thread, i am a multithreading newbie by the way. here is the code

#include <iostream>
#include <boost/thread/thread.hpp>
#include <windows.h>

using namespace std;

void Avg(double * Src, double *Dst, int Per, int Len, string& s )
{
    LARGE_INTEGER s1,s2,f;
    QueryPerformanceFrequency(&f);
    QueryPerformanceCounter(&s1);
    for(int i = Per-1; i < Len ; i++)
    {
        double a = 0;
        for(int j = i; j > i-Per ; j--)
            a += Src[j];
        Dst[i] = a / Per;
    }       
    QueryPerformanceCounter(&s2);
   cout << double(s2.QuadPart-s1.QuadPart)/f.QuadPart*1000 << " ms : "+s << endl;
}

int main(int argc, char* argv[])
{
    int L = 200000;
    double  * a = new double[L], *b = new double[L] , *c = new double[L];
    for(int i =0; i < L;i++)
    {
        a[i] = i+2;
    }
    int x = 10000;
    boost::thread bAvg(Avg,a,b,x,L,string("T"));
    LARGE_INTEGER s1,s2,f;
    QueryPerformanceFrequency(&f);
    QueryPerformanceCounter(&s1);

    Avg(a,c,x,L,string("N")); // line 1
    bAvg.join(); // line 2

    QueryPerformanceCounter(&s2);
    cout << double(s2.QuadPart-s1.QuadPart)/f.QuadPart*1000 << " ms : Total" << endl;


    delete []a;
    delete []b;
    delete []c;

    system("PAUSE");
         return 0;
}

the output of that code is

6621.1 ms : N
6635.28 ms : T
6638.29 ms : Total
Press any key to continue . . .

But when i change the order of line 1 and line 2 the output becomes:

6274.57 ms : T
6250.56 ms : N
12531.3 ms : Total
Press any key to continue . . .

in case 1, bAvg.join() is fired just after Avg fired and before it is finished. i thought the results of case 1 and case 2 would be the opposite of each other. this has something to do with the execution order of threads i think.

edit:
actually i am planning to write an application for trading purposes. i am going to create, say, at least 10 threads of calculations per upcoming signal for a single stock. the signals will be received via tcp connection. but if the main execution thread waits for the completion of other sub-threads’, then a single signal may be easily missed, because the main thread is idle. how am i supposed to handle the signals and run the calculation threads?

  • 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-05-16T14:12:58+00:00Added an answer on May 16, 2026 at 2:12 pm

    In the first case, order may be both N T and T N, because both functions are executed in parallel. In the second case, output can be only T N, because the first function (T) must finish before the second (N) starts. bAvg.join means “Wait for thread function to exit”.

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

Sidebar

Related Questions

I have written very very simple console application which supports some command line options.
i have a simple .EXE console application, written in C# via Visual Studio 2008.
I have a simple console application written in .NET. I need to make an
I have simple console application in C++ written in Qt Creator 2.4.1 on Kubuntu
I have built a simple console application in c++ vs2010 that just displays one
I have written simple code to get content from xml file to php. $xml
I have written a simple automation script for deploying and restarting my twisted application
I have written a simple web Application which is running(on Tomcat Server) fine on
I have written a simple Java dispatcher with a daemon thread to handle the
I have a simple standalone application written in Visual Basic that I'm porting to

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.