#include<pthread.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<stdio.h>
#include<math.h>
#include<sys/uio.h>
#include<time.h>
#include<string.h>
#include<sys/timeb.h>
#include <netinet/tcp.h>
#include <semaphore.h>
#include <signal.h>
#include <sys/syscall.h>
volatile int lock = 0;
int glbint = 0 ;
struct Mars_
{
int glbintx1 ;
char pad[64] ;
int glbintx2 ;
};
struct Mars_ Mval ;
pthread_mutex_t Mutex1 = PTHREAD_MUTEX_INITIALIZER;
pthread_spinlock_t spinlock ;
pthread_spinlock_t spinlock1 ;
pthread_spinlock_t spinlock2 ;
pid_t gettid( void )
{
return syscall( __NR_gettid );
}
void *thread1F(void *param)
{
int i ;
int proc_num = (int)(long)param ;
cpu_set_t set;
CPU_ZERO( &set );
CPU_SET( proc_num, &set );
if (sched_setaffinity( gettid(), sizeof( cpu_set_t ), &set ))
{
perror( "sched_setaffinity" );
return NULL;
}
for(i=0;i<100000000;i++)
{
pthread_spin_lock(&spinlock1);
Mval.glbintx1 = Mval.glbintx1 + 1 ;
pthread_spin_unlock(&spinlock1);
/*
pthread_spin_lock(&spinlock);
glbint = glbint + 1 ;
pthread_spin_unlock(&spinlock);
*/
/*
pthread_mutex_lock(&Mutex1);
glbint = glbint + 1 ;
pthread_mutex_unlock(&Mutex1);
*/
/*
__sync_add_and_fetch(&glbint, 1) ;
*/
/*
while (__sync_lock_test_and_set(&lock, 1))
while (lock)
;
glbint = glbint + 1 ;
__sync_lock_release(&lock);
*/
}//for
}
void *thread2F(void *param)
{
int i ;
int proc_num = (int)(long)param ;
cpu_set_t set;
CPU_ZERO( &set );
CPU_SET( proc_num, &set );
if (sched_setaffinity( gettid(), sizeof( cpu_set_t ), &set ))
{
perror( "sched_setaffinity" );
return NULL;
}
for(i=0;i<100000000;i++)
{
pthread_spin_lock(&spinlock2);
Mval.glbintx2 = Mval.glbintx2 + 1 ;
pthread_spin_unlock(&spinlock2);
/*
pthread_spin_lock(&spinlock);
glbint = glbint + 1 ;
pthread_spin_unlock(&spinlock);
*/
/*
pthread_mutex_lock(&Mutex1);
glbint = glbint + 1 ;
pthread_mutex_unlock(&Mutex1);
*/
/*
__sync_add_and_fetch(&glbint, 1) ;
*/
/*
while (__sync_lock_test_and_set(&lock, 1))
while (lock)
;
glbint = glbint + 1 ;
__sync_lock_release(&lock);
*/
}//for
}
int main (int argc, char *argv[])
{
pthread_t tid[2] ;
pthread_spin_init(&spinlock, 0);
pthread_spin_init(&spinlock1, 0);
pthread_spin_init(&spinlock2, 0);
pthread_create(&tid[0] , NULL, thread1F, (void*)(long)0);
pthread_create(&tid[1] , NULL, thread2F, (void*)(long)1);
pthread_join(tid[0], NULL );
pthread_join(tid[1], NULL );
printf("glbint=(%d)\n",glbint) ;
printf("glbintx=(%d)(%d)\n",Mval.glbintx1,Mval.glbintx2) ;
}
留言列表