基本多线程有问题 [英] having trouble with basic multithreading

查看:61
本文介绍了基本多线程有问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力学习多线程,而且似乎并没有为我工作。我有一种感觉,这与我正在写文件

而不是printf这一事实有关,但也许不是。基本上,我想看看它是否可以更快地同时写入4个文件(并行),而不是连续写入4个文件(串行)。但是,当我的多线程代码执行时,无论如何,它似乎按顺序执行它们(我希望看到所有的开始/结束

混合而不是按顺序)。有谁知道我做错了什么? (注意,

我也尝试用4种不同的功能(比如go,go1,go2等等......,

但这似乎没有修复它。


请参阅下面的代码并在下面输出。

#include" stdio.h"

#include< ; pthread.h> / * pthread函数和数据结构* /


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''\ nn,name);

pthread_exit(0);

}

int main()

{

int thr_id,thr_two,thr_three,thr_four; / * thread

新创建的线程* /

pthread_t p_thread,thread2,thread3,thread4; / * thread''s

结构* /

char a1 [] =" a1";

char b1 [] =" b1" ;;

char c1 [] =" c1";

char d1 [] =" d1";

printf(" ;创建线程...... \ n");

thr_id = pthread_create(& p_thread,NULL,go,(void *)& a1);

printf (创建线程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);

返回0;

}

cc -mt MultiTest.c -o MultiTest


/ export / CUST / systems / dan / process / development / MultiThread> ./MultiTest

创建线程......

创建线程2. ..

创建线程3. ..

创建线程4. ..

开始''a1''

结束''a1''

开始''b1''

结束''b1''

开始''c1''

结束''c1''

开始''d1''

结束''d1''

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


推荐答案

In文章< j5 ********************* @ twister.nyc.rr.com>,

Winbatch< wi *** ***@techie.com>写道:

:我正在努力学习多线程,它似乎不适用于

:我。

线程不是C本身的一部分。


有多个竞争线程包。在我看来,

你想要的是找到一个知道某事的讨论区

关于POSIX线程。

:编写起来会更快同时4个文件(并行)而不是

:连续4个(连续)。但是,当我的多线程代码执行时,它好像无论如何都要按顺序执行它们(我希望看到开始/结束所有

:混合而不是按顺序)。有谁知道我做错了什么?


创建一个帖子需要多长时间,而不是需要多少时间才能执行这项工作在线程中?

-

我一直在内核工作

所有活泼的夜晚。

我一直在研究内核

它仍然不能正常工作。 - J. Benson& J. Doll
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写道:
Winbatch wrote:
我正在努力学习多线程,它似乎并没有为
Hi, I''m trying to learn multithreading and it doesn''t seem to be working for




[snip]

请你接受新闻:comp.programming.threads。 ISO标准C没有概念

多线程。


HTH,

--ag


-

Artie Gold - 德克萨斯州奥斯汀
http://it-matters.blogspot.com (新帖子12/5)
http://www.cafepress.com/goldsays



[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


Winbatch写道:
Winbatch wrote:
我正在努力学习多线程,但似乎并不是
为我工作。我有一种感觉,这与我写的
文件而不是printf的事实有关,但也许不是。基本上,我想看看
是否可以更快地同时写入4个文件(并行)
而不是连续写入4个(串行)。但是,当我的多线程代码执行时,
它似乎按顺序执行它们(我希望看到开始/结束
全部混合而不是按顺序)。有谁知道我做错了什么?
(注意,我也尝试用4种不同的功能(比如go,go1,go2,
等等),但这似乎无法修复它。

请参阅下面的代码并在下面输出。
#include" stdio.h"
#include< pthread.h> / * pthread函数和数据结构* /

void * go(void * data1)
{
long i = 0;
FILE * file;
char * name;
name =(char *) data1;
printf(" Starting''%s''\ nn,name);
file = fopen(name," w");

for (i = 0; i <10000000; i ++)
fprintf(文件,%ld \ n,i);
}
fclose(file);
printf(" Ending''%s''\ n,name);
pthread_exit(0);

}
int main()
{int / thr,thr_two,thr_three,thr_four; / *
newl的线程ID y创建线程* /
pthread_t p_thread,thread2,thread3,thread4; / * thread'的结构* /
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);
返回0;
}

cc -mt Mu ltiTest.c -o MultiTest

/ export / CUST / systems / dan / process / development / MultiThread> ./MultiTest
创建线程......
创建线程2. ..
创建线程3. ..
创建线程4. ..
启动''a1 ''
结束''a1''
开始''b1''
结束''b1''
开始''c1''
结束''c1 ''
开始''d1''
结束''d1''
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''




试试这个。

#包括< stdio.h>

#include< windows.h>


#define MAX_M 4

//定义最大文件打开

//线程函数

DWORD WINAPI ThFunc(PVOID pvParam){

DWORD dwResult = 0;

char * name;

long i = 0;

FILE * file;


name =(char *)pvParam;


printf(" Starting''%s''\ nn,quot; name);

file = fopen(name," ; w");


for(i = 0; i< 10000000; i ++)

{

fprintf(file, "%ld\\\
" ;,i);

}

fclose(文件);


return(dwResult);

}


VOID主(VOID){


int cnt;

int redak [MAX_M];

char a1 [] =" a1";

char b1 [] =" b1";

char c1 [] =" ; c1" ;;

char d1 [] =" d1" ;;


HANDLE hThreads [MAX_M]; //处理线程

//查看CreateThread的原型

函数)

DWORD dwThreadID [MAX_M]; //需要ID-s


//创建线程1(0)查看参数

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);


//等到他们完成工作,等待INFINITE

WaitForMultipleO bjects(MAX_M,hThreads,TRUE,INFINIT E);


//关闭线程句柄

for(cnt = 0; cnt< MAX_M; cnt ++){

printf(" Closing thread%d .... \ n",cnt);

CloseHandle(hThreads [cnt]);

}

}

我希望我能帮到你,如果你对这个问题有更多的疑问

代码请发给我邮件。


最好的问候

Sinisa



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


这篇关于基本多线程有问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆