how to multithread using c++

I

iceman

Hi,
I need to implement a timer associated with a state[in a state
machine].In case the statemachine does not get any input for a
particular period of time, the timer should reset and there should be
default action.
How do I implement multiple threads in c++[in linux].

I got this example on the web .its uses 2 threads.but it does not
execute together.
thread 1 executes then thread 2

void *task1(void *X);
void *task2(void *X);

int main(int argc, char *argv[])
{
pthread_t ThreadA,ThreadB;
int N;

if(argc != 2){
std::cout << "error" <<"\n";
exit (1);
}

N = atoi(argv[1]);
pthread_create(&ThreadA,NULL,task1,&N);
pthread_create(&ThreadB,NULL,task2,&N);
std::cout << "waiting for threads to join" <<"\n";
pthread_join(ThreadA,NULL);
pthread_join(ThreadB,NULL);
return(0);
}



void *task1(void *X)
{
int *Temp;
Temp = static_cast<int *>(X);

for(int Count = 1;Count < *Temp;Count++){
std::cout << "work from thread A: " << Count << " * 2 = "
<< Count * 2 <<"\n";
}
std::cout << "Thread A complete" <<"\n";
}

void *task2(void *X)
{
int *Temp;
Temp = static_cast<int *>(X);

for(int Count = 1;Count < *Temp;Count++){
std::cout << "work from thread B: " << Count << " + 2 = "
<< Count + 2 <<"\n";
}
std::cout << "Thread B complete" << "\n";

}
 
I

Ian Collins

iceman said:
Hi,
I need to implement a timer associated with a state[in a state
machine].In case the statemachine does not get any input for a
particular period of time, the timer should reset and there should be
default action.
How do I implement multiple threads in c++[in linux].

I got this example on the web .its uses 2 threads.but it does not
execute together.
thread 1 executes then thread 2
Do you have more than one core? If not, what else would you expect?
 
I

iceman

iceman said:
Hi,
I need to implement a timer associated with a state[in a state
machine].In case the statemachine does not get any input for a
particular period of time, the timer should reset and there should be
default action.
How do I implement multiple threads in c++[in linux].
I got this example on the web .its uses 2 threads.but it does not
execute together.
thread 1 executes then thread 2

Do you have more than one core? If not, what else would you expect?

I want the both the threads to execute together.not one after the
other.yep its a single core.
Is there anyway to do this on a single core
 
I

Ian Collins

iceman said:
iceman said:
Hi,
I need to implement a timer associated with a state[in a state
machine].In case the statemachine does not get any input for a
particular period of time, the timer should reset and there should be
default action.
How do I implement multiple threads in c++[in linux].
I got this example on the web .its uses 2 threads.but it does not
execute together.
thread 1 executes then thread 2
Do you have more than one core? If not, what else would you expect?
*Please* don't quote signatures.
I want the both the threads to execute together.not one after the
other.yep its a single core.
Is there anyway to do this on a single core

No, not on a single core, how could they?
 
J

Jeff Schwab

iceman said:
iceman said:
Hi,
I need to implement a timer associated with a state[in a state
machine].In case the statemachine does not get any input for a
particular period of time, the timer should reset and there should be
default action.
How do I implement multiple threads in c++[in linux].

<off-topic>
There's no standard, portable way to do it, but you might have a look at
the POSIX threading library "pthreads." If you really just want the
program to receive a signal from Linux after a certain amount of time
has occurred, you can request a SIGALRM. See:

man sigaction
man pthread

>
> I want the both the threads to execute together.not one after the
> other.yep its a single core.
> Is there anyway to do this on a single core

Not on most cores, depending on what you mean by "together." Are you
seeing a long-running thread complete before the second thread even
starts? If you're only running very short jobs, one thread finishing
before the other might be normal. If you're running long threads (more
than a few seconds of CPU time each, or I/O-bound jobs), you should see
the first one run for a bit, then the second, then the first one again,
and so on. For example, if each of two threads prints its ID to
standard output repeatedly (e.g. in a long-running loop), you might see
something like:

<thread 1>
<thread 1>
<thread 1>
<thread 2>
<thread 2>
<thread 1>
<thread 1>
<thread 2>
<thread 2>
<thread 2>
 
I

iceman

iceman said:
iceman wrote:
Hi,
I need to implement a timer associated with a state[in a state
machine].In case the statemachine does not get any input for a
particular period of time, the timer should reset and there should be
default action.
How do I implement multiple threads in c++[in linux].

<off-topic>
There's no standard, portable way to do it, but you might have a look at
the POSIX threading library "pthreads." If you really just want the
program to receive a signal from Linux after a certain amount of time
has occurred, you can request a SIGALRM. See:

man sigaction
man pthread

I want the both the threads to execute together.not one after the
other.yep its a single core.
Is there anyway to do this on a single core

Not on most cores, depending on what you mean by "together." Are you
seeing a long-running thread complete before the second thread even
starts? If you're only running very short jobs, one thread finishing
before the other might be normal. If you're running long threads (more
than a few seconds of CPU time each, or I/O-bound jobs), you should see
the first one run for a bit, then the second, then the first one again,
and so on. For example, if each of two threads prints its ID to
standard output repeatedly (e.g. in a long-running loop), you might see
something like:

<thread 1>
<thread 1>
<thread 1>
<thread 2>
<thread 2>
<thread 1>
<thread 1>
<thread 2>
<thread 2>
<thread 2>





yep..thanks a lot guys
 
S

Szabolcs Ferenczi

iceman said:
I got this example on the web .its uses 2 threads.but it does not
execute together.
thread 1 executes then thread 2

That is correct behaviour in the example you have shown. At least one
possible legal behaviour. Running disjoint threads, the OS can
schedule them this way too. And after all, that is what is specified
in the code. I mean by disjoint threads that they are independent from
each other, they do not have to rendezvous in order to carry out a
joint task. All they have to do is, which is missing from the example,
by the way, to exclude each other when using some shared resource. So
the output operation should be protected by a mutex at least,
otherwise even two of your output lines from the different threads can
be merged together. And if this happens you cannot object, since the
code just allows it.

Best Regards,
Szabolcs
 
J

Juha Nieminen

Ian said:
Do you have more than one core? If not, what else would you expect?

Even with one single core, if both threads take 1 hour to complete
their calculations, I would not expect the system to run first one
thread, and when it finishes, then the other.

Multithreading is very useful even in single-core machines. (For
example GUI'ed programs which perform heavy calculations use it a lot,
to keep the GUI responsive during the calculations.)
 
I

Ian Collins

Juha said:
Even with one single core, if both threads take 1 hour to complete
their calculations, I would not expect the system to run first one
thread, and when it finishes, then the other.
Did you see the OP's code?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top