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 2094
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 thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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.
...
|
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...
|
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...
|
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)
{...
|
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...
|
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...
|
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.
...
|
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...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
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...
|
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: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |