- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
int cont[15];
bool stop_access = false;
struct cel container[MAX_SIZE];
struct termios savetty;
struct termios tty;
pthread_t thread[100];
pthread_mutexattr_t muattr;
pthread_mutex_t count_mutex;
int icn=0;//Number elements in container
static int x;
static int y;
void* stack;
pthread_mutex_unlock(&count_mutex);
sleep(1);
};
return (void*)1;
};
void* main_thread(void *arg){
while(true){
sleep(1);
char ic=getchar();
if(ic=='\n'){
printf("Thread\tnumber elements\n");
for(int i=0;i<=x;i++)
pthread_cancel(thread[i]);
for(int i=0;i<=x;i++){
printf("%d ",i);
int cn=0;
for(int j=0;j<=icn;j++){
if(container[j].thrnm==i){ cn++; };
};
printf("\t%d\n",cn);
};
printf("container number element %d\n",icn);
exit(0);
};
};
};
int main(int argc, char * argv[])
{
icn=0;
if(argc<2){printf("1 arg n number of threads, 2 argument maximum number element in container");exit(0);};
x=atoi(argv[1]);
y=atoi(argv[2]);
if(x>64){printf("Число потоков должно быть меньше 64\n");exit(0);}
printf("x %d y %d\n",x,y);
//mutex initialization
int ret;
ret = pthread_mutexattr_init(&muattr);
//switch the keyboard to noncanonical mode
pid_t pt=tcgetsid(0);
// char *var=ctermid (NULL);
printf("pid %d\n",pt);
tcgetattr (0, &tty);
savetty = tty;
tty.c_lflag &= ~(ICANON);
tty.c_cc[VMIN] = 1;
tcsetattr (0, TCSAFLUSH, &tty);
pthread_t mthr;
pthread_attr_t mattr;
pthread_attr_init(&mattr);
pthread_attr_setdetachstate(&mattr,PTHREAD_CREATE_DETACHED);
int mres = pthread_create(&mthr, &mattr, main_thread, NULL);
if (mres == 0) {
printf("Creating main thread\n");
sleep(0.7);
} else {
perror("Creating the main first thread");
return EXIT_FAILURE;
}
for(int i=0;i<=x;i++){
int id1, id2, result;
id1 = 1;
pthread_attr_t attr;
pthread_attr_init(&attr);
// отсоединенный поток - не ждем его возврата
pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
result = pthread_create(&thread[i], &attr, thread_func, &i);
if (result == 0) {
printf("Creating thread %d\n",i);
sleep(1);
} else {
perror("Creating the first thread");
AliceGoth 14.04.2011 19:25 # 0
RaZeR 14.04.2011 20:56 # −1
guest 14.04.2011 21:27 # +5
Lure Of Chaos 14.04.2011 21:30 # 0
TheCalligrapher 14.04.2011 21:54 # +3