传递到pthread_create的值已更改
我正在使用pthread_create和互斥量来处理线程,但我注意到在我的简单示例中,我存储在传递到pthread_create的结构中的int值在线程执行SimpleThread方法时不会保留其值。下面是代码。具体地说,在第63行中,我将循环的计数分配给结构线程中的int id,该结构线程用作pthread_create中的参数。在第22行,我从结构中打印出id的值,它总是给出相同的值。如果我创建2个线程,则值为2。如果创建3个线程,则值为3。这种模式还在继续。我想我只是好奇为什么会发生这种情况,而不是得到第63行中我想要的实际值传递到pthread_create的值已更改,c,pthreads,C,Pthreads,我正在使用pthread_create和互斥量来处理线程,但我注意到在我的简单示例中,我存储在传递到pthread_create的结构中的int值在线程执行SimpleThread方法时不会保留其值。下面是代码。具体地说,在第63行中,我将循环的计数分配给结构线程中的int id,该结构线程用作pthread_create中的参数。在第22行,我从结构中打印出id的值,它总是给出相同的值。如果我创建2个线程,则值为2。如果创建3个线程,则值为3。这种模式还在继续。我想我只是好奇为什么会发生这种情
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <pthread.h>
4 #include <unistd.h>
5
6 #define PROGRAM_NAME 0
7 #define NUM_THREADS 1
8
9 int SharedVariable;
10 pthread_mutex_t mutex;
11
12 struct thread
13 {
14 pthread_t t;
15 int id;
16 };
17
18 void* SimpleThread( void* arg )
19 {
20 struct thread* parameter = ( struct thread* ) arg;
21 int which = parameter->id;
22 printf( "Threads IDs in SimpleThread -- %d\n", parameter->id );
23
24 pthread_mutex_lock( &mutex );
25
26 int num, val;
27
28 for( num = 0; num < 20; num++ )
29 {
30 if( random( ) > RAND_MAX / 2 )
31 usleep( 10 );
32
33 val = SharedVariable;
34
35 //printf( "***thread %d sees value %d\n", which, val );
36 SharedVariable = val + 1;
37 }
38
39 val = SharedVariable;
40 //printf( "Thread %d sees final value %d\n", which, val );
41
42 pthread_mutex_unlock( &mutex );
43
44 return ( void * ) arg;
45 }
46
47 int main( int argc, char* argv[ ] )
48 {
49 int num_threads, i;
50
51 if( argc != 2 )
52 {
53 fprintf( stderr, "Usage: %s <num_threads>\n", argv[ PROGRAM_NAME ] );
54 return -1;
55 }
56 num_threads = atoi( argv[ NUM_THREADS ] );
57 struct thread* container = ( struct thread* ) malloc( num_threads * sizeof( struct thread ) );
58
59 pthread_mutex_init( &mutex, NULL );
60
61 for( i = 0; i < num_threads; i++ )
62 {
63 container[ i ].id = i;
64 pthread_create( &container[ i ].t, 0, SimpleThread, &container );
65 }
66
67 for( i = 0; i < num_threads; i++ )
68 {
69 pthread_join( container[ i ].t, 0 );
70 }
71
72 pthread_mutex_destroy( &mutex );
73
74 return 0;
75 }
1#包括
2#包括
3#包括
4#包括
5.
6#定义程序名称0
7#定义NUM#u线程1
8.
9整数共享变量;
10 pthread_mutex_t mutex;
11
12结构线程
13 {
14 pthread_t t;
15 int id;
16 };
17
18 void*SimpleThread(void*arg)
19 {
20结构线程*参数=(结构线程*)参数;
21 int其中=参数->id;
22 printf(“SimpleThread中的线程id--%d\n”,参数->id);
23
24 pthread_mutex_lock(&mutex);
25
26 int num,val;
27
28表示(num=0;num<20;num++)
29 {
30如果(随机()>RAND_MAX/2)
31个美国LEEP(10个);
32
33 val=共享变量;
34
35//printf(“***线程%d看到值%d\n”,其中,val);
36共享变量=val+1;
37 }
38
39 val=共享变量;
40//printf(“线程%d看到最终值%d\n”,其中,val);
41
42 pthread_mutex_unlock(&mutex);
43
44返回(无效*)参数;
45 }
46
47 int main(int argc,char*argv[])
48 {
49个int num_线程,i;
50
51如果(argc!=2)
52 {
53 fprintf(标准,“用法:%s\n”,argv[程序名称];
54返回-1;
55 }
56 num_threads=atoi(argv[num_threads]);
57结构线程*容器=(结构线程*)malloc(num_线程*sizeof(结构线程));
58
59 pthread_mutex_init(&mutex,NULL);
60
61表示(i=0;i
您将相同的地址作为参数提供给两个不同的线程。您可以通过这种方式将其作为共享资源。您可以将相同的地址作为参数提供给两个不同的线程。您可以通过这种方式将其作为共享资源。您没有将正确的数组元素传递到线程中
而不是
pthread_create( &container[ i ].t, 0, SimpleThread, &container )
你需要这样做
pthread_create( &container[ i ].t, 0, SimpleThread, &container[i] )
您没有将正确的数组元素传递到线程中 而不是
pthread_create( &container[ i ].t, 0, SimpleThread, &container )
你需要这样做
pthread_create( &container[ i ].t, 0, SimpleThread, &container[i] )
struct-thread*参数=(struct-thread*)参数代码>,然后struct-thread*container=(struct-thread*)malloc
-aaaaaaaaaaaaaarrrrr gggggghhh!也不要强制转换malloc。我试图删除malloc,但在输出后出现分段错误。我的理解是,main函数中的malloc初始化了结构,然后我只使用初始化的结构作为SimpleThread函数中的参数代码>,然后struct-thread*container=(struct-thread*)malloc
-aaaaaaaaaaaaaarrrrr gggggghhh!也不要强制转换malloc。我试图删除malloc,但在输出后出现分段错误。我的理解是,main函数中的malloc初始化了结构,然后我只使用初始化的结构作为SimpleThread函数中的参数。