Connecting Tech Pros Worldwide Forums | Help | Site Map

having trouble with basic multithreading

Winbatch
Guest
 
Posts: n/a
#1: Nov 14 '05
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'





Walter Roberson
Guest
 
Posts: n/a
#2: Nov 14 '05

re: having trouble with basic multithreading


In article <j5UTd.13771$qn2.3088263@twister.nyc.rr.com>,
Winbatch <winbatch@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
Artie Gold
Guest
 
Posts: n/a
#3: Nov 14 '05

re: having trouble with basic multithreading


Winbatch wrote:[color=blue]
> Hi, I'm trying to learn multithreading and it doesn't seem to be working for[/color]

[snip]
Take ye to news:comp.programming.threads. ISO standard C has no concept
of multithreading.

HTH,
--ag

--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays
sipetar@inet.hr
Guest
 
Posts: n/a
#4: Nov 14 '05

re: having trouble with basic multithreading


Winbatch wrote:[color=blue]
> Hi, I'm trying to learn multithreading and it doesn't seem to be[/color]
working for[color=blue]
> me. I have a feeling it has to do with the fact that I'm writing to[/color]
files[color=blue]
> rather than to printf, but maybe not. Basically, I wanted to see if[/color]
it[color=blue]
> would be faster to write to 4 files at the same time (parallel)[/color]
rather than[color=blue]
> 4 in a row (serially). however, when my multithreaded code executes,[/color]
it[color=blue]
> seems to do them in order anyway (I expected to see Starting/Ending[/color]
all[color=blue]
> mixed rather than in order). Does anyone know what I'm doing wrong?[/color]
(Note,[color=blue]
> I also tried to do it with 4 different functions (ie, go, go1, go2,[/color]
etc..,[color=blue]
> 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; /*[/color]
thread[color=blue]
> 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'[/color]

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

sipetar@inet.hr
Guest
 
Posts: n/a
#5: Nov 14 '05

re: having trouble with basic multithreading



Me wrote->
[color=blue]
> int redak[MAX_M];[/color]

I have use this for something else...so...you dont need that...



Svako rješenje donosi još problema....

Joona I Palaste
Guest
 
Posts: n/a
#6: Nov 14 '05

re: having trouble with basic multithreading


sipetar@inet.hr scribbled the following:[color=blue]
> Winbatch wrote:[color=green]
>> Hi, I'm trying to learn multithreading and it doesn't seem to be[/color]
> working for[color=green]
>> me. I have a feeling it has to do with the fact that I'm writing to[/color]
> files[color=green]
>> rather than to printf, but maybe not. Basically, I wanted to see if[/color]
> it[color=green]
>> would be faster to write to 4 files at the same time (parallel)[/color]
> rather than[color=green]
>> 4 in a row (serially). however, when my multithreaded code executes,[/color]
> it[color=green]
>> seems to do them in order anyway (I expected to see Starting/Ending[/color]
> all[color=green]
>> mixed rather than in order). Does anyone know what I'm doing wrong?[/color]
> (Note,[color=green]
>> I also tried to do it with 4 different functions (ie, go, go1, go2,[/color]
> etc..,[color=green]
>> 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 */[/color][/color]
[color=blue][color=green]
>> cc -mt MultiTest.c -o MultiTest
>>
>> /export/CUST/systems/dan/process/development/MultiThread> ./MultiTest[/color][/color]
[color=blue]
> Try this.
> #include<stdio.h>
> #include<windows.h>[/color]
[color=blue]
> #define MAX_M 4
> // define max file to open
> // thread function
> DWORD WINAPI ThFunc(PVOID pvParam){
> DWORD dwResult = 0;[/color]

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 (palaste@cc.helsinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"Holy Banana of this, Sacred Coconut of that, Magic Axolotl of the other."
- Guardian in "Jinxter"
CBFalconer
Guest
 
Posts: n/a
#7: Nov 14 '05

re: having trouble with basic multithreading


sipetar@inet.hr wrote:[color=blue]
> Winbatch wrote:
>[color=green]
>> 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[/color][/color]
.... snip ...[color=blue]
>
> I hope I have helped you and if you have futher questions about this
> code send me them by mail.[/color]

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


Winbatch
Guest
 
Posts: n/a
#8: Nov 14 '05

re: having trouble with basic multithreading


[color=blue]
> [snip]
> Take ye to news:comp.programming.threads. ISO standard C has no concept of
> multithreading.[/color]

I have posted there, hopefully someone will be able to help. ( I also
modified the subject to indicate that I am on solaris..)




Jens.Toerring@physik.fu-berlin.de
Guest
 
Posts: n/a
#9: Nov 14 '05

re: having trouble with basic multithreading


Winbatch <winbatch@techie.com> wrote:
[color=blue][color=green]
>> [snip]
>> Take ye to news:comp.programming.threads. ISO standard C has no concept of
>> multithreading.[/color][/color]
[color=blue]
> I have posted there, hopefully someone will be able to help. ( I also
> modified the subject to indicate that I am on solaris..)[/color]

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 ___ Jens.Toerring@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Mark McIntyre
Guest
 
Posts: n/a
#10: Nov 14 '05

re: having trouble with basic multithreading


On Sat, 26 Feb 2005 05:49:03 GMT, in comp.lang.c , "Winbatch"
<winbatch@techie.com> wrote:
[color=blue]
>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[/color]

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 =----
Winbatch
Guest
 
Posts: n/a
#11: Nov 14 '05

re: having trouble with basic multithreading



"Mark McIntyre" <markmcintyre@spamcop.net> wrote in message
news:vrr121pde4demasuhtk1k3dshes7qtlam2@4ax.com...[color=blue]
> On Sat, 26 Feb 2005 05:49:03 GMT, in comp.lang.c , "Winbatch"
> <winbatch@techie.com> wrote:
>[color=green]
>>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[/color]
>
> 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
> =----[/color]

On a UNIX system with different disks, I don't see why that would be so
difficult...


Mark McIntyre
Guest
 
Posts: n/a
#12: Nov 14 '05

re: having trouble with basic multithreading


On Sun, 27 Feb 2005 03:11:20 GMT, in comp.lang.c , "Winbatch"
<winbatch@techie.com> wrote:
[color=blue]
>
>"Mark McIntyre" <markmcintyre@spamcop.net> wrote in message
>news:vrr121pde4demasuhtk1k3dshes7qtlam2@4ax.com.. .[color=green]
>> 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...[/color]
>
>On a UNIX system with different disks, I don't see why that would be so
>difficult...[/color]

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 =----
Emmanuel Delahaye
Guest
 
Posts: n/a
#13: Nov 14 '05

re: having trouble with basic multithreading


sipetar@inet.hr wrote on 26/02/05 :[color=blue]
> #include<stdio.h>
> #include<windows.h>[/color]

<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.

Closed Thread


Similar C / C++ bytes