|
||||||
| Forums FAQ | Members List | Search | Today's Posts | Mark Forums Read |
| ARCHIVES - Programming & Scripting A place to discuss website design, programming, shell scripts, etc |
|
|
LinkBack | Thread Tools | Display Modes |
|
|||
|
I have a problem with pthreads, and I am not sure whether I am doing something wrong. I am win32 programmer who's just learning Linux.
I did a small test program just to see how threads on Linux behave. More importantly I wanted to see how threads are scheduled across multiple CPUs depending on their number etc... However, running single thread in addition to main process thread slows down program execution by a factor of roughly 10. I have checked through KDE System Guard, runing a load graph on both CPU cores, and apparently both are active (for more than 1 thread in the process), yet 10 times slower. The same app on Windows, but with CreateThread instead of pthread_create (the only difference, except the headers) runs fine and expectedly fast. I am assuming I am compiling the thing wrong or something. I am also aware there are, like, three threading libs on Linux? Linux Threads, NGLT and NPLT. How can I compile for NPLT? Or is this it? So many questions. ![]() Anyways, here is the test code. Please ignore any lack of elegance, this is just a test app. It requires two parameters, first being the number of threads to use and the second being the size of memory block to go through in the hogging function (this is to test the CPU caches). If you run it with 1 thread, that one will be the main process thread and no additional thread will be launched. The Code: Code:
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
char *globmem;
int MEMSIZE;
int hogfunc() {
**int i, j;
**float z;
**for (i=0; i<100000; i++) {
****j= (int) (((float) (rand()) / RAND_MAX) * MEMSIZE);
****z= exp(globmem[j]) * sqrt(i+1+globmem[j]);
**}
}
void* print_xs (void *threadno) {
**int i, c;
**c= (int) threadno;
**c+= 49;
**for (i=0; i<200; i++) {
****hogfunc();
****fputc (c, stderr);
**}
**return NULL;
}
int main(int argc, char **argv) {
**pthread_t thread_id;
**int i, j, nthreads;
**float z, exectime;
**clock_t start, end;
**start= clock();
**if (argc!=3)
****return 1;
**
**nthreads= atoi (argv[1]);
**MEMSIZE= atoi (argv[2]);
**globmem= (char*) malloc(MEMSIZE);
**if (globmem==NULL) {
****fprintf (stderr, "Unable to malloc!");
****return 1;
**}
**for (i=1; i<nthreads; i++) {
****pthread_create (&thread_id, NULL, &print_xs, (void*) i);
**}
**print_xs((void*) 0);
**free(globmem);
**end= clock();
**exectime= (float) (end - start) / CLOCKS_PER_SEC;
**fprintf (stderr, "\nExec time: %.4f sec\n", exectime);
**return 0;
}
Code:
g++ -o threads -lm -lpthread -m64 -O3 -D_REENTRANT threads.c Code:
./threads 2 512000 OpenSuSE 10.3 (with all updates) 4.2.1 gcc and g++ with all devel libs, both 32 and 64 bit AMD Turion 64 X2 2GB RAM (1GB+ free when running the app) I did a test forking a process instead of spawning a thread, and it works as expected, but I need threads. Thanks for any help I can get! ![]() EDIT: The KDE Sys Guard shows that when this is run with 1 thread (only the main process thread), it utilizes the CPU 100% for about 3 seconds. When run with more than 1 thread, it utilizes the CPU 100% for 30+ seconds, but 60% is user load and 40% is system load on both cores. What does that mean? |
|
|||
|
I can't say I know the answer, as I'm not much of a coder, but the O'Reilly Posix IV book might help you out. They also have other books on that website about pthreads that you could look into.
http://www.oreilly.com/catalog/posix4/ |
|
|||
|
Thanks for the reply, but that's not an issue. I am familiar with POSIX threads, I am just not familiar with compiling multithreaded apps on Linux. In fact the program up above is from one of the tutorials on POSIX threads, but they don't give you compiler options!
The books you suggest and majority of documentation on the net is TOO OLD. Various libraries exist for various kernel versions apparently all with same interface - pthreads. Aparently NPLT is the latest, and present in the libc that comes with OpenSuse 10.3. I guess libc version 6 works with NPLT library which is latest and best performing. I am sure that I am doing something wrong, possibly missing compiler options, because I refuse to believe that a threated operation on Windows that takes less than one second with 4 threads, takes almost a minute on Linux on the same architecture! |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|