473,388 Members | 1,125 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

having trouble with basic multithreading

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'


Nov 14 '05 #1
12 2090
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
Nov 14 '05 #2
Winbatch wrote:
Hi, I'm trying to learn multithreading and it doesn't seem to be working for


[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
Nov 14 '05 #3
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

Nov 14 '05 #4

Me wrote->
int redak[MAX_M];


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

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

Nov 14 '05 #5
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"
Nov 14 '05 #6
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
Nov 14 '05 #7
[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..)


Nov 14 '05 #8
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
Nov 14 '05 #9
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 =----
Nov 14 '05 #10

"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...
Nov 14 '05 #11
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 =----
Nov 14 '05 #12
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.

Nov 14 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: asfwa | last post by:
I'm new to C++ and I have some basic questions. I have written an app that does some network stuff in a worker thread. The thread function requests something from the server, gets it and creates...
1
by: Fernando Rodríguez | last post by:
Hi, I don't the difference between the wait|sleep|join states and the suspended one. Could someone please explain this? O:-) Any pointers to multithreading tutorials would be appreciated to. ...
5
by: sarge | last post by:
I would like to know how to perform simple multithreading. I had created a simple form to test out if I was multithreading properly, but got buggy results. Sometime the whole thig would lock up...
1
by: Jozef | last post by:
Hello. I'm having trouble creating a blank solution (and ASP.net web application) from my laptop. I own the server (in fact it's sitting right next to me) and have added the URL to the trusted...
8
by: Flack | last post by:
Hey guys, In my app I have a bitmap where drawing is done and in the form's paint method I show the bitmap: private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) {...
2
by: sameer | last post by:
Hi guys, thanks in advance. trying to implement some multithreading here : VS 2003 VB.net winforms I have a dropdown on a form which has list of vendors, when the user selects a vendor...
7
by: Ray | last post by:
Hello, Greetings! I'm looking for a solid C++ multithreading book. Can you recommend one? I don't think I've seen a multithreading C++ book that everybody thinks is good (like Effective C++ or...
19
by: frankiespark | last post by:
Hello all, I was perusing the internet for information on threading when I came across this group. Since there seems to be a lot of good ideas and useful info I thought I'd pose a question. ...
2
by: Anish G | last post by:
Hi, I am new to working on threading. I need to implement multithreading for calling four functions (each functions are having parameters) in different threads so that i can improve the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.