LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-15-2006, 02:33 PM   #1
mvorlov
LQ Newbie
 
Registered: Nov 2005
Posts: 11

Rep: Reputation: 0
Threads execution question


Hi,
I'm trying to set my program running in two threads on two CPU's in parallel. It reports that both treads have started successfully, and yet only the first thread runs on one CPU. However if I add sleep(1) statement in the beginning of the thread function I am able to see both threads run in parallel taking two CPU's. Can someone explain me what why is this happening?

Thanks is advance and here is the code I am referring to.

// ===============================================

#pragma ident "$Id$" // M.O.
const char* version = "v1.0 20060915";

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

extern "C" {
void* thread_function1(void* arg);
void* thread_function2(void* arg);
}


int
main(int argc, char **argv)
{
const int threads = 2;
pthread_t thread_data[threads];
void* thread_result[threads];

int arg[threads];

int t = 0;
arg[t] = t;
if (pthread_create(&thread_data[t], NULL, thread_function1, (void*)&arg[t])) {
printf("ERROR: Thread #%d creation failed\n", t);
return 1;
}
else
printf("Thread %d created\n", t);

arg[++t] = t;
if (pthread_create(&thread_data[t], NULL, thread_function2, (void*)&arg[t])) {
printf("ERROR: Thread #%d creation failed\n", t);
return 1;
}
else
printf("Thread %d created\n", t);


printf("Waiting for thread(s) to finish...\n");

int errors = 0;
for (int t = 0; t < threads; t++) {
if (pthread_join(thread_data[t], &thread_result[t])) {
printf("ERROR: Thread #%d join failed\n", t + 1);
return 1;
}

int res = *(int*)(thread_result[t]);
errors += !res;

printf("Thread #%d joined, it returned %d\n", t + 1, res);
}

printf("ERRORS: %d\n", errors);

return 0;
}


void*
thread_function1(void* arg)
{
int thread_id = *(int*)arg;
printf("Entering thread #%d\n", thread_id);
//sleep(1);

while (1)
continue;

static int exit_status = 1;
pthread_exit((void*)(&exit_status));

return NULL;
}


void*
thread_function2(void* arg)
{
int thread_id = *(int*)arg;
printf("Entering thread #%d\n", thread_id);
//sleep(1);

while (1)
continue;

static int exit_status = 1;
pthread_exit((void*)(&exit_status));

return NULL;
}
 
Old 09-17-2006, 08:45 AM   #2
randyding
Member
 
Registered: May 2004
Posts: 552

Rep: Reputation: 31
Take a look at my previous posting on this. Then if you still have a question let us know.
http://www.linuxquestions.org/questi...88#post2386588
 
Old 09-17-2006, 08:08 PM   #3
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939
Even if you have two CPUs, it's entirely up to the system dispatcher as to which thread runs where, when.

Bear in mind that if a thread does any I/O, as apparently both of your threads do, this significantly affects their execution because both of them are now firmly tied to the single resource .. a virtual terminal.
 
Old 09-18-2006, 02:45 AM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
You have to give the threads some actual 'real work' to do, or the 1st thread will just spin in 1 cpu and the other will never really get started.
As soon as they start doing any kind of eg disk or net i/o, you should get both going, but you can use the 'yield()' routine to hint to the OS that it should give the other thead(s) a chance.
however, as sundialsvcs said above, it's really down to the OS/kernel.
 
Old 09-18-2006, 08:08 AM   #5
mvorlov
LQ Newbie
 
Registered: Nov 2005
Posts: 11

Original Poster
Rep: Reputation: 0
randyding:
I read you post... I still do not understand why kernel is not utilizing all available resources on 4 CPU machine...

sundialsvcs:
Interesting observation: If I replace while(1) loop with 'for' loop, thread 2 starts eventually (with like one minute delay). I have other program that uses 2 threads, both of which use alot of I/O from different file descriptors. That program works fine. The only difference I can think of is that the second program has each CPU utilization slightly below 100% due to the I/O wait...

chrism01:
I wrote this dummy code to simulate and debug my other program which does alot of in-memory computations without I/O and fails to take advantage of the dual-core machine. I'm basically trying to recreate the environment as close as possible to understand the nature of this weird behavior.
 
Old 09-18-2006, 10:04 AM   #6
mvorlov
LQ Newbie
 
Registered: Nov 2005
Posts: 11

Original Poster
Rep: Reputation: 0
Interesting... Just tried to pthread_attr_setschedpolicy to SCHED_RR and now both threads are running taking up 2 cpu's...
 
Old 09-18-2006, 03:13 PM   #7
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939
You're worrying too much about the scheduler, and your latest change actually interferes with what it's doing by ordering it to schedule them "round-robin."

Here's my friendly advice: let the scheduler do its job, and you do yours. (I don't mean any offense nor anything offensive by that: it's just an expression.) Once you've got two threads working, and if you've given them both reasonable jobs to do, the scheduler will see to it that the threads get a fair share of available CPU time and they will also see to it that the CPUs are kept busy.

When your system basically doesn't have any workload, and/or the threads aren't doing anything, you're never going to see an accurate picture. The situation is too artificial. Frankly, you're focusing on this (non-)issue much too much. (Again, no offense!) Spend your time now on telling the threads what to do.
 
Old 09-19-2006, 08:13 AM   #8
mvorlov
LQ Newbie
 
Registered: Nov 2005
Posts: 11

Original Poster
Rep: Reputation: 0
Well, the only reason I got involved with multiple threads was to decrease the total execution time by distributing almost identical function calls over two cores. When I did that scheduler still used only one core of the CPU and execution time in fact increased. So, I was wondering why is it not using the other core.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Question about threads (I don't understand how they work) zahadumy Programming 10 12-13-2005 12:19 PM
Thorny remote execution question davidcrawley Linux - Software 3 09-03-2004 02:32 PM
question about subscribtion to threads lyceum LQ Suggestions & Feedback 8 07-09-2004 01:15 PM
question about threads Jo_Nak Programming 1 08-23-2003 11:43 AM
maximum # of threads question amar Linux - General 5 06-04-2003 06:30 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:27 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration