having trouble with basic multithreading

W

Winbatch

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'
 
W

Walter Roberson

: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?
 
A

Artie Gold

Winbatch said:
Hi, I'm trying to learn multithreading and it doesn't seem to be working for

[snip]
Take ye to ISO standard C has no concept
of multithreading.

HTH,
--ag
 
S

sipetar

Winbatch said:
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,INFINITE);

//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
 
J

Joona I Palaste

(e-mail address removed) scribbled the following:
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.
 
C

CBFalconer

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

Winbatch

[snip]
Take ye to 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..)
 
J

Jens.Toerring

Winbatch said:
[snip]
Take ye to 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
 
M

Mark McIntyre

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

Winbatch

Mark McIntyre said:
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...
 
M

Mark McIntyre

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..... :)
 
E

Emmanuel Delahaye

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

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,780
Messages
2,569,611
Members
45,277
Latest member
VytoKetoReview

Latest Threads

Top