Hi, I'm trying to learn multithreading and it doesn't seem to be working for
me. I have a feeling it has to do with the fact that I'm writing to files
rather than to printf, but maybe not. Basically, I wanted to see if it
would be faster to write to 4 files at the same time (parallel) rather than
4 in a row (serially). however, when my multithreaded code executes, it
seems to do them in order anyway (I expected to see Starting/Ending all
mixed rather than in order). Does anyone know what I'm doing wrong? (Note,
I also tried to do it with 4 different functions (ie, go, go1, go2, etc..,
but that didn't seem to fix it).
See code below and output below.
#include "stdio.h"
#include <pthread.h> /* pthread functions and data structures */
void * go ( void * data1 )
{
long i=0;
FILE * file;
char * name;
name = (char *) data1;
printf( "Starting '%s'\n", name );
file = fopen( name, "w" );
for ( i=0;i< 10000000;i++)
{
fprintf( file, "%ld\n", i );
}
fclose(file);
printf( "Ending '%s'\n", name );
pthread_exit(0);
}
int main()
{
int thr_id, thr_two, thr_three, thr_four; /* thread
ID for the newly created thread */
pthread_t p_thread, thread2, thread3,thread4; /* thread's
structure */
char a1[]="a1";
char b1[]="b1";
char c1[]="c1";
char d1[]="d1";
printf( "Creating threads...\n" );
thr_id = pthread_create(&p_thread, NULL, go, (void*)&a1);
printf( "Creating thread 2. ..\n" );
thr_two = pthread_create(&thread2, NULL, go, (void*)&b1);
printf( "Creating thread 3. ..\n" );
thr_three = pthread_create(&thread3, NULL, go, (void*)&c1);
printf( "Creating thread 4. ..\n" );
thr_four = pthread_create(&thread4, NULL, go, (void*)&d1);
pthread_join(p_thread, 0);
pthread_join(thread2, 0);
pthread_join(thread3, 0);
pthread_join(thread4, 0);
return 0;
}
cc -mt MultiTest.c -o MultiTest
/export/CUST/systems/dan/process/development/MultiThread> ./MultiTest
Creating threads...
Creating thread 2. ..
Creating thread 3. ..
Creating thread 4. ..
Starting 'a1'
Ending 'a1'
Starting 'b1'
Ending 'b1'
Starting 'c1'
Ending 'c1'
Starting 'd1'
Ending 'd1' 12 1989
In article <j5*********************@twister.nyc.rr.com>,
Winbatch <wi******@techie.com> wrote:
:Hi, I'm trying to learn multithreading and it doesn't seem to be working for
:me.
Threads are not part of C itself.
There are multiple competing thread packages. Looks to me that
what you want is to find a discussion area that knows something
about POSIX threads.
:would be faster to write to 4 files at the same time (parallel) rather than
:4 in a row (serially). however, when my multithreaded code executes, it
:seems to do them in order anyway (I expected to see Starting/Ending all
:mixed rather than in order). Does anyone know what I'm doing wrong?
How long does it take to create a thread, vs the time it takes to
execute the work in the thread?
--
I've been working on a kernel
All the livelong night.
I've been working on a kernel
And it still won't work quite right. -- J. Benson & J. Doll
Winbatch wrote: Hi, I'm trying to learn multithreading and it doesn't seem to be
working for me. I have a feeling it has to do with the fact that I'm writing to
files rather than to printf, but maybe not. Basically, I wanted to see if
it would be faster to write to 4 files at the same time (parallel)
rather than 4 in a row (serially). however, when my multithreaded code executes,
it seems to do them in order anyway (I expected to see Starting/Ending
all mixed rather than in order). Does anyone know what I'm doing wrong?
(Note, I also tried to do it with 4 different functions (ie, go, go1, go2,
etc.., but that didn't seem to fix it).
See code below and output below. #include "stdio.h" #include <pthread.h> /* pthread functions and data structures */
void * go ( void * data1 ) { long i=0; FILE * file; char * name; name = (char *) data1; printf( "Starting '%s'\n", name ); file = fopen( name, "w" );
for ( i=0;i< 10000000;i++) { fprintf( file, "%ld\n", i ); } fclose(file); printf( "Ending '%s'\n", name ); pthread_exit(0);
} int main() { int thr_id, thr_two, thr_three, thr_four; /*
thread ID for the newly created thread */ pthread_t p_thread, thread2, thread3,thread4; /* thread's structure */ char a1[]="a1"; char b1[]="b1"; char c1[]="c1"; char d1[]="d1"; printf( "Creating threads...\n" ); thr_id = pthread_create(&p_thread, NULL, go, (void*)&a1); printf( "Creating thread 2. ..\n" ); thr_two = pthread_create(&thread2, NULL, go, (void*)&b1); printf( "Creating thread 3. ..\n" ); thr_three = pthread_create(&thread3, NULL, go, (void*)&c1); printf( "Creating thread 4. ..\n" ); thr_four = pthread_create(&thread4, NULL, go, (void*)&d1); pthread_join(p_thread, 0); pthread_join(thread2, 0); pthread_join(thread3, 0); pthread_join(thread4, 0); return 0; }
cc -mt MultiTest.c -o MultiTest
/export/CUST/systems/dan/process/development/MultiThread> ./MultiTest Creating threads... Creating thread 2. .. Creating thread 3. .. Creating thread 4. .. Starting 'a1' Ending 'a1' Starting 'b1' Ending 'b1' Starting 'c1' Ending 'c1' Starting 'd1' Ending 'd1'
Try this.
#include<stdio.h>
#include<windows.h>
#define MAX_M 4
// define max file to open
// thread function
DWORD WINAPI ThFunc(PVOID pvParam){
DWORD dwResult = 0;
char * name ;
long i=0;
FILE * file;
name =( char*)pvParam;
printf( "Starting '%s'\n", name );
file = fopen( name, "w" );
for ( i=0;i< 10000000;i++)
{
fprintf( file, "%ld\n", i );
}
fclose(file);
return(dwResult);
}
VOID main(VOID){
int cnt;
int redak[MAX_M];
char a1[]="a1";
char b1[]="b1";
char c1[]="c1";
char d1[]="d1";
HANDLE hThreads[MAX_M];// handles to threads
//see prototype of CreateThread
function)
DWORD dwThreadID[MAX_M];// need for ID-s
// create thread 1(0) see arguments
hThreads[0] = CreateThread(NULL,0,ThFunc,(PVOID)a1,0,&dwThreadID );
hThreads[1] = CreateThread(NULL,0,ThFunc,(PVOID)b1,0,&dwThreadID );
hThreads[2] = CreateThread(NULL,0,ThFunc,(PVOID)c1,0,&dwThreadID );
hThreads[3] = CreateThread(NULL,0,ThFunc,(PVOID)d1,0,&dwThreadID );
// wait until they finish their job , wait INFINITE
WaitForMultipleObjects(MAX_M,hThreads,TRUE,INFINIT E);
//closing handles to thread
for(cnt=0; cnt < MAX_M; cnt++){
printf("Closing thread %d ....\n",cnt);
CloseHandle(hThreads[cnt]);
}
}
I hope I have helped you and if you have futher questions about this
code send me them by mail.
Best regards
Sinisa
Me wrote-> int redak[MAX_M];
I have use this for something else...so...you dont need that...
Svako rješenje donosi još problema.... si*****@inet.hr scribbled the following: Winbatch wrote: Hi, I'm trying to learn multithreading and it doesn't seem to be working for me. I have a feeling it has to do with the fact that I'm writing to files rather than to printf, but maybe not. Basically, I wanted to see if it would be faster to write to 4 files at the same time (parallel) rather than 4 in a row (serially). however, when my multithreaded code executes, it seems to do them in order anyway (I expected to see Starting/Ending all mixed rather than in order). Does anyone know what I'm doing wrong? (Note, I also tried to do it with 4 different functions (ie, go, go1, go2, etc.., but that didn't seem to fix it).
See code below and output below. #include "stdio.h" #include <pthread.h> /* pthread functions and data structures */
cc -mt MultiTest.c -o MultiTest
/export/CUST/systems/dan/process/development/MultiThread> ./MultiTest
Try this. #include<stdio.h> #include<windows.h>
#define MAX_M 4 // define max file to open // thread function DWORD WINAPI ThFunc(PVOID pvParam){ DWORD dwResult = 0;
This is why system-specific questions are off-topic on comp.lang.c. The
OP's message strongly hints that he is on a UNIX box, however the reply
contains Windows code. I don't think it's going to work.
--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"Holy Banana of this, Sacred Coconut of that, Magic Axolotl of the other."
- Guardian in "Jinxter" si*****@inet.hr wrote: Winbatch wrote:
Hi, I'm trying to learn multithreading and it doesn't seem to be working for me. I have a feeling it has to do with the fact that
.... snip ... I hope I have helped you and if you have futher questions about this code send me them by mail.
You have helped nobody by answering off-topic questions and failing
to redirect it to a competent newsgroup. Your answer is
meaningless because there is nobody here (at least in theory) to
criticize it. Meanwhile you have just wasted bandwidth.
--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson [snip] Take ye to news:comp.programming.threads. ISO standard C has no concept of multithreading.
I have posted there, hopefully someone will be able to help. ( I also
modified the subject to indicate that I am on solaris..)
Winbatch <wi******@techie.com> wrote: [snip] Take ye to news:comp.programming.threads. ISO standard C has no concept of multithreading.
I have posted there, hopefully someone will be able to help. ( I also modified the subject to indicate that I am on solaris..)
Since this isn't a C issue (you're using some system specific exten-
sions and your problem seems to be with these, not C) your chances
here are rater slim. Did you already try in comp.unix.programmer or
comp.unix.solaris? While I don't read comp.unix.solaris I know that
multi-threaded programming is a topic that comes up regularly in
comp.unix.programmer.
Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
On Sat, 26 Feb 2005 05:49:03 GMT, in comp.lang.c , "Winbatch"
<wi******@techie.com> wrote: Basically, I wanted to see if it would be faster to write to 4 files at the same time (parallel) rather than 4 in a row (serially). however, when my multithreaded code executes, it seems to do them in order anyway
I'm wondering how you expected the disk drive to move the head to different
4 places at the same time...
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==---- http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
"Mark McIntyre" <ma**********@spamcop.net> wrote in message
news:vr********************************@4ax.com... On Sat, 26 Feb 2005 05:49:03 GMT, in comp.lang.c , "Winbatch" <wi******@techie.com> wrote:
Basically, I wanted to see if it would be faster to write to 4 files at the same time (parallel) rather than 4 in a row (serially). however, when my multithreaded code executes, it seems to do them in order anyway
I'm wondering how you expected the disk drive to move the head to different 4 places at the same time... -- Mark McIntyre CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html> CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==---- http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
On a UNIX system with different disks, I don't see why that would be so
difficult...
On Sun, 27 Feb 2005 03:11:20 GMT, in comp.lang.c , "Winbatch"
<wi******@techie.com> wrote: "Mark McIntyre" <ma**********@spamcop.net> wrote in message news:vr********************************@4ax.com.. . On Sat, 26 Feb 2005 05:49:03 GMT, in comp.lang.c , "Winbatch" I'm wondering how you expected the disk drive to move the head to different 4 places at the same time...
On a UNIX system with different disks, I don't see why that would be so difficult...
If you read what I said carefully, you'll notice that the number of disk
drives is immaterial..... :-)
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==---- http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =---- si*****@inet.hr wrote on 26/02/05 : #include<stdio.h> #include<windows.h>
<snipped...>
Cute:
main.c:30: warning: return type of `main' is not `int' main.c: In
function `main':
main.c:46: warning: passing arg 6 of `CreateThread' from incompatible
pointer type main.c:47: warning: passing arg 6 of `CreateThread' from
incompatible pointer type
main.c:48: warning: passing arg 6 of `CreateThread' from incompatible
pointer type
main.c:49: warning: passing arg 6 of `CreateThread' from incompatible
pointer type
main.c:33: warning: unused variable `redak'
--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html
"Mal nommer les choses c'est ajouter du malheur au
monde." -- Albert Camus. This discussion thread is closed Replies have been disabled for this discussion. Similar topics
7 posts
views
Thread by asfwa |
last post: by
|
1 post
views
Thread by Fernando Rodríguez |
last post: by
|
5 posts
views
Thread by sarge |
last post: by
|
1 post
views
Thread by Jozef |
last post: by
|
8 posts
views
Thread by Flack |
last post: by
|
2 posts
views
Thread by sameer |
last post: by
|
7 posts
views
Thread by Ray |
last post: by
|
19 posts
views
Thread by frankiespark |
last post: by
| | | | | | | | | | | |