473,797 Members | 3,148 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(th read2, 0);
pthread_join(th read3, 0);
pthread_join(th read4, 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 2133
In article <j5************ *********@twist er.nyc.rr.com>,
Winbatch <wi******@techi e.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.progr amming.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(th read2, 0);
pthread_join(th read3, 0);
pthread_join(th read4, 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<window s.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(NU LL,0,ThFunc,(PV OID)a1,0,&dwThr eadID);
hThreads[1] = CreateThread(NU LL,0,ThFunc,(PV OID)b1,0,&dwThr eadID);
hThreads[2] = CreateThread(NU LL,0,ThFunc,(PV OID)c1,0,&dwThr eadID);
hThreads[3] = CreateThread(NU LL,0,ThFunc,(PV OID)d1,0,&dwThr eadID);

// wait until they finish their job , wait INFINITE
WaitForMultiple Objects(MAX_M,h Threads,TRUE,IN FINITE);

//closing handles to thread
for(cnt=0; cnt < MAX_M; cnt++){
printf("Closing thread %d ....\n",cnt);
CloseHandle(hTh reads[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<window s.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.hel sinki.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.c om, 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.progr amming.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******@techi e.com> wrote:
[snip]
Take ye to news:comp.progr amming.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.progr ammer or
comp.unix.solar is? While I don't read comp.unix.solar is I know that
multi-threaded programming is a topic that comes up regularly in
comp.unix.progr ammer.
Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@p hysik.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******@techi e.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

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

Similar topics

7
1727
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 an object from the server response. Does this thread terminate/stop/die as soon as it completes its last line of code? Or do I have to do something to kill it? I am used to coding Java so I am totally unsure of what I have to do to "clean up"...
1
1222
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. Thanks :-)
5
2143
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 when I got two threads going at the same time. What I have is two text boxes (textBox1 and textBox2) and four buttons(cmdStartThread1, cmdStartThread2, cmdStopThread1, cmdStopThread2)
1
1640
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 sites on my laptop. Here are the details; This is what I'm selecting from the start page.... >Add New Blank Solution >Visual Basic Projects,
8
7680
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) { if(MyBitmap != null) { Graphics g = e.Graphics;
2
1233
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 from this dropdown, a new thread is created which calls a webservice and the webservice returns the list of cities the vendor is in and this data
7
16315
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 Exceptional C++, for example). Platform-specific (e.g.: Win32, POSIX) is OK, as long as it's good :) Thank you, Ray
19
1811
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. Threading is a new concept for me to implement. Here is my problem. I have a system that receives xml files and records their file locations in a database. I can potentially receive thousands,
2
1342
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 performance of the software. Can anybody help me with sample code for implementing this? Thanks, Anish.
0
9536
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10245
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10205
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10021
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6802
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5458
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4131
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2933
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.